[[oktatas:web:angular|< Angular]] ====== Angular - Tömb lapozása ====== * **Szerző:** Sallai András * Copyright (c) 2024, Sallai András * Szerkesztve: 2024 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== Szolgáltatás ===== Az src/app/app.config.ts fájlban regisztrálni kell a HttpClientModule modult, amit nem másoltam ide. import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class ApiService { host = 'http://localhost:8000/' constructor(private http: HttpClient) { } getEmployees() { let endpoint = 'employees'; let url = this.host + endpoint; return this.http.get(url); } } ===== HTML =====

Dolgozók

@for(emp of pagedEmployees; track emp) { }
Id Név Település Fizetés
{{emp.id}} {{emp.name}} {{emp.city}} {{emp.salary}}
===== TypeScript ===== import { Component } from '@angular/core'; import { ApiService } from '../shared/api.service'; @Component({ selector: 'app-employee', standalone: true, imports: [], templateUrl: './employee.component.html', styleUrl: './employee.component.css' }) export class EmployeeComponent { employees : any = [] pageSize = 5; currentPage = 1; constructor(private api: ApiService){ api.getEmployees().subscribe({ next: data => { console.log(data); this.employees = data; }, error: err => { console.error('Hiba! A letöltés sikertelen!'); } }) } get totalPages(): number { return Math.ceil(this.employees.length / this.pageSize); } get pagedEmployees(): any { const startIndex = (this.currentPage - 1) * this.pageSize; return this.employees.slice(startIndex, startIndex + this.pageSize); } previousPage() { this.currentPage--; } nextPage() { this.currentPage++; } }