angular - 将 `combineLatest` 与 Angular 材质表一起使用
问题描述
尝试combineLatest
与 Angular Material一起使用Table
。试图将MatPaginator
and the MatSort
and 结合起来是行不通的。
这是我正在关注的示例。我正在查看“通过 HTTP 检索数据的表”示例。
这是我正在尝试做的事情:
@ViewChild(MatPaginator) private _paginator: MatPaginator;
@ViewChild(MatSort) private _sort: MatSort;
public ngOnInit(): void {
this._sort.sortChange.subscribe(
() => {
console.log("this works");
}
);
combineLatest(
this._sort.sortChange,
this._paginator.page
).pipe(
startWith({}),
switchMap(
() => {
return this._userService.getAll();
}
),
tap(
(users: IUser[]) => {
this._data = users;
}
)
).subscribe();
}
上面代码的问题是combineLatest
observable 只在组件加载时触发一次。我希望它在每次触发排序或分页时触发。当我直接订阅sortChange
时,每次我更改排序时它都会触发。
当我更改combineLatest
为merge
. 它将按预期工作。但是每次更改任何一个时,我都需要结合排序器和分页器的最新结果。但它永远不会与combineLatest
. 这里发生了什么?
解决方案
您正在将结果combineLatest
传递给startWith
操作员,该操作员只会发出一次。只需省略startWith
运算符,而不是将结果设置在运算符中,而是tap
使用subscribe
函数,如下所示:
@ViewChild(MatPaginator) private _paginator: MatPaginator;
@ViewChild(MatSort) private _sort: MatSort;
public ngOnInit(): void {
this._sort.sortChange.subscribe(
() => {
console.log("this works");
}
);
merge(combineLatest(
this._sort.sortChange,
this._paginator.page), of({}))
.pipe(switchMap(() => this._userService.getAll()))
.subscribe((users: IUser[]) => this._data = users);
}
推荐阅读
- localhost - 如何从本地主机上的应用程序中获取 burp catch 流?
- http - 将 URL 中的用户名和密码传递给“htpasswd”受保护的域,它会被加密吗?
- ionic-framework - 如何使用 Ionic-4 中的路由导航同一页面?
- angular - 如何向 ngStyle 添加和删除单个属性?
- sonarqube - 声纳服务器未启动
- vue.js - 我如何知道泛事件已在 nativescript-vue 中结束?
- maven - 在为 xml 单元创建依赖项时在 pom.xml 中出现错误
- php - Laravel 有片段缓存吗?
- java - 如何按条件从队列中删除消息?
- reactjs - 注销时如何在 React Native 中删除异步存储中每个键的值