0

I'm new at angular so I have this issue.

<div class="col-md-4" *ngFor="let student of students | paginate: {itemsPerPage:5, currentPage: p, totalItems: len}">
     {{student.name}}
</div>
<pagination-controls (pageChange)="p =$event"></pagination-controls>

I'm getting this error

Cannot read property 'totalItems' of undefined at PaginatePipe.push../node_modules/ngx-pagination/dist/ngx-pagination.js.PaginatePipe.transform

Component code:

public students= [];
public len;
ngOnInit() {
    this.serverService.getStudents().subscribe(
      data => {
        this.students= data,
        this.len = this.students.length
      }
    );
  }
txenone
  • 1
  • 1
  • 3

3 Answers3

0
<tbody *ngFor= "let item of con?.data?.countries | paginate:{itemsPerPage:10,currentPage: p}">
    <tr>
        <td>{{item['name']}}</td>
        <td>{{item['code']}}</td>                             
    </tr>
</tbody>
Milo
  • 3,365
  • 9
  • 30
  • 44
0

I don't think you need to use totalItems as the pagination property here. ngx-pagination will automatically consider the length of the list/array as the total items.

<div class="col-md-4" *ngFor="let student of students | paginate: {itemsPerPage:5, currentPage: p}">
     {{student.name}}
</div>
<pagination-controls (pageChange)="p =$event"></pagination-controls>

This will resolve your original issue/error:

Cannot read property 'totalItems' of undefined at PaginatePipe.push../node_modules/ngx-pagination/dist/ngx-pagination.js.PaginatePipe.transform

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
-1
I am working at Angular 8. This code is helpful to use the 
pagination. I declare base url in API_URL in environment.ts.
example: API_URL:"http....",
endpoint: country

HTML code:
<tbody *ngFor= "let item of con?.data?.countries 
|paginate:itemsPerPage:10,currentPage: p}">
<tr>
<td>{{item['name']}}</td>
<td>{{item['code']}}</td>                             
</tr>
</tbody>

example.component.ts:

con:any;

ngOnInit() {
this.http.get(`${environment.API_URL}/country`).subscribe(res=>{
this.con=res      
})
}