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
Id
Név
Település
Fizetés
@for(emp of pagedEmployees; track emp) {
{{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++;
}
}