I have 2 components. Both have mat-table and paginators and pagination is working for one component and not working for the other component though the code is similar. Below
Using async-await worked for me inside ngOnInit(), the paginator and sort has to wait!
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
.
.
.
ngOnInit() {
this.isLoading = true;
this._statsService.getAllCampgrounds().subscribe(
async (response) => {
this.allCampgrounds = response.allCampgrounds;
this.dataSource = await new MatTableDataSource(this.allCampgrounds);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.isLoading = false;
},
(error) => {
this.isLoading = false;
console.log(error);
}
);
}