奇怪的是,他们是如何构建这些组件的。我不确定我的解决方案是否有效,但根据我在这篇文章中读到的内容,他们似乎无法同时处理这两个问题
length
改变和
pageIndex
在同一消化周期中发生变化。
因此,我建议更改
长
首先,然后再更改
当前页
.
this.paginator.length = this.dataSource.data.length;
this.paginator.pageIndex = 0;
this.dataSource.paginator = this.paginator;
console.log('made it this far');
上面设置了我们知道将对组件起作用的值,下一部分使用
window.setTimeout
在组件有机会更新自身后运行下一组更改。
if (this.dataSource.paginator.pageIndex < currentPage) {
window.setTimeout(()=>{
this.zone.run(()=>{
console.log('need to return to the current page');
this.paginator.pageIndex = currentPage;
this.paginator._pageIndex = currentPage;
this.dataSource.paginator.pageIndex = currentPage;
this.dataSource.paginator._pageIndex = currentPage;
// EDIT: You must then set the datasource paginator back to the newly edited paginator.
this.dataSource.paginator = this.paginator;
});
});
}
你需要注射
NgZone
在Angular的区域内运行代码(用于错误处理)。这只是使用
窗设置超时
.
记住
window
在Angular Universal中不存在,以防以后需要进行服务器端渲染,但这是另一个问题。