angular - Angular Take 操作员需要退订吗?
问题描述
我试图像这样过滤我的角度材料表,但它不起作用(可能是因为它是一个 Observable)。
HTML
<mat-form-field>
<mat-label>Search</mat-label>
<input matInput placeholder="Digite algo a ser apresentado (keyup)="toSearch($event.target)" />
<mat-icon matSuffix>search</mat-icon>
</mat-form-field>
TS
aplicativos:any = this.aplicativoService.toGetAllAplicativos();
dataSource = new MatTableDataSource();
ngOnInit(): void {
this.dataSource = this.aplicativos;
}
toSearch = (search: any) =>
(this.dataSource.filter = search.value.trim().toLowerCase());
服务:
toGetAllAplicativos() { return this.http.get(`${this.API}/aplicativos`).pipe(take(1)); }
所以我改变了我的代码,让它像这样工作,现在它正在工作:
TS
aplicativos = this.aplicativoService.toGetAllAplicativos();
dataSource;
ngOnInit(): void {
this.aplicativos.subscribe(response => {
this.dataSource = new MatTableDataSource(response);
})
}
toSearch(search: any){
this.dataSource.filter = search.value.trim().toLowerCase();
}
问题是:如果我使用 take(1),我需要订阅吗?
解决方案
不,如果您使用take(1)
or first()
,则不需要取消订阅,因为它会在收到第一个事件后自动取消订阅。
更详细的答案在这里
推荐阅读
- asp.net-core - 在 ASPNET Core 的路由中使用 ApiExplorerSettings GroupName
- python - 将pandas df中的列名和索引旋转到列本身的有效方法,对应的值作为第三列?
- laravel - 使用 Laravel 7 Eloquent 对关系创建和保存添加限制
- r - R/RStudio 无法运行,出现循环 socketConnection 错误
- python - 如何使用 Python-Zeroconf 发送数据
- r - 将多个表格和绘图的列表导出为 pdf 和 excel 文件
- c++ - 为什么 getline 在 3 个换行符之后表现异常?
- apache-spark - TIMESTAMP 列问题 CDH5 与镶木地板表中的 CDH6
- c++ - C++:在初始化列表中,类成员按什么顺序更新并传递给基类构造函数(在它被调用之前)?
- python - Jupyter Notebook 保存运行输出