angular - 在 TS 中使用 Custom_Pipe
问题描述
我有这个管道:
@Pipe({
name: 'searchNomES'
})
export class SearchNomESPipe implements PipeTransform {
transform(uos: IUo[], name?: string,): IUo[] {
if (!uos) return [];
if (!name) return uos;
name = name.toLocaleLowerCase();
uos = [...uos.filter(uo => uo.nom.toLocaleLowerCase().includes(name))];
return uos;
}
}
当我像这样在我的 html 中使用管道时,它工作正常:
<ng-container *cdkVirtualFor="let image of display | async | searchNomES : name " >
</ng-container>
但我尝试在我的 component.ts 中使用管道。我试试这个:
<mat-form-field >
<input matInput
(keyup)="applyFilter2($event.target.value)">
</mat-form-field>
import { SearchNomESPipe } from '../../search-nomES.pipe';
constructor(private espipe: SearchNomESPipe) { }
ngOnInit() {this.display=this.markerservice.getGeos() }
applyFilter2(name : string) {
this.display = this.espipe.transform(this.display,name);
}
我的服务:
getGeos() { return this.
database.list('ES').snapshotChanges().pipe(map(actions => {
return actions.map(a => {
const data = a.payload.val();
const key = a.payload.key;
return {key, ...data };
但我有这个错误:
uos.filter 不是函数或其返回值不可迭代
解决方案
您正在使用可观察对象,因此您的管道必须处理可观察对象并返回可观察对象。然后,您将async
在视图中使用管道。将您的管道修改为:
transform(uos: Observable<IUo[]>, name?: string): Observable<IUo[]> {
return uos.pipe(
map(data => {
if (!data || !name) return [];
name = name.toLocaleLowerCase();
return data.filter(uo => uo.title.toLocaleLowerCase().includes(name));
})
);
}
然后模板:
<ng-container *cdkVirtualFor="let image of filtered | async" >
TS:
display: Observable<IUo[]>;
filtered: Observable<IUo[]>;
ngOnInit() {
this.display=this.markerservice.getGeos()
}
applyFilter2(name : string) {
this.filtered = this.espipe.transform(this.display,name);
}
推荐阅读
- jmeter - 我创建了一个由 10 个用户组成的步进线程组,但在运行计划后它没有显示用户数,也没有停止在 10 个用户
- popupwindow - 弹出窗口显示另一个程序正在使用此文件的错误
- html - 使第二行文本在第一行文本下对齐
- django - Django - 我正在尝试将用户对象添加到 ManyToManyField,但它不断添加所有用户对象
- amazon-web-services - Kinesis 视频流 - 多个生产者
- swift - Swift - AES base64 + md5 解密
- codenameone - 如何将多个容器绑定在一起?
- javascript - 如何克隆网页及其 css 元素和脚本?
- node.js - 如何通过不同的api使用socket io连接服务器和客户端?
- javascript - Discord.js Avatar 命令无法提及用户