使用 viewChild 信号后无法更改 Angular Materials 分页器值

问题描述 投票:0回答:1

我需要更改我的表分页器 pageIndex 和 pageSize,当我使用 ViewChild 时,我的代码运行良好,但我想使用信号查询,当我使用信号查询时,我收到此错误;

默认情况下,在

computed
effect
中不允许写入信号。

///@ViewChild('paginator') paginator: MatPaginator; -->this works
paginator = viewChild<MatPaginator>('paginator');

_onPaginationChange(event: PageEvent) {
    const previousPageIndex = this.req.pageNumber - 1;
    const previousPageSize = this.req.pageSize;

    this.checkModifiedCellsAndCallback(() => {
      this.req.pageNumber = event.pageIndex + 1;
      this.req.pageSize = event.pageSize;
      this.getTableData();
    });

    // Revert paginator state if the user cancels the action
    if (this.modifiedCells.size > 0) {
      setTimeout(() => {
        paginator.pageIndex = previousPageIndex;
        paginator.pageSize = previousPageSize;
      });
    }
  }
angular angular-material viewchild angular-signals
1个回答
0
投票

您必须执行 signal 方法才能从

viewChild
访问该元素。

paginator = viewChild<MatPaginator>('paginator');

_onPaginationChange(event: PageEvent) {
    const previousPageIndex = this.req.pageNumber - 1;
    const previousPageSize = this.req.pageSize;

    this.checkModifiedCellsAndCallback(() => {
      this.req.pageNumber = event.pageIndex + 1;
      this.req.pageSize = event.pageSize;
      this.getTableData();
    });

    // Revert paginator state if the user cancels the action
    if (this.modifiedCells.size > 0) {
      setTimeout(() => {
        const paginator = this.paginator();
        paginator.pageIndex = previousPageIndex;
        paginator.pageSize = previousPageSize;
      });
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.