![](/img/trans.png)
[英]How do I trigger the (page) event on a mat-paginator only when pageIndex changes in Angular?
[英]How to pass pageIndex in mat-paginator to API in Angular 9
.html文件
<mat-paginator [pageSize]="10" [pageSizeOptions]="[5, 10, 25]"></mat-paginator>
.ts 文件
listCustomer() {
this.customerservice
.getCustomer(this.pageSize, this.paginator.pageIndex + 1)
.subscribe((res) => {
this.list = res["success"].data;
this.dataSource = new MatTableDataSource(this.list);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
});
}
core.js:4061 ERROR TypeError: Cannot read property 'pageIndex' of undefined
我該如何解決這個問題?
這是stackblatz上的示例
.getCustomer(this.pageSize, this.paginator.pageIndex + 1)
好像this.paginator是null
你必須使用viewChild來獲取對分頁器的引用
// component
@ViewChild('paginatorComp') paginator: MatPaginator;
// html
<mat-paginator #paginatorComp [pageSize]="10" [pageSizeOptions]="[5, 10, 25]"> </mat-paginator>
如果您使用 viewChild 可能需要實現 AfterViewInit 循環鈎子並將分頁器使用到此鈎子中。 因為進入 OnInit 你還不能訪問這些數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.