Angular MatPaginator not working

前端 未结 30 1568
情歌与酒
情歌与酒 2020-12-04 23:05

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

30条回答
  •  渐次进展
    2020-12-05 00:07

    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);
          }
        );
      }
    

提交回复
热议问题