我有用于过滤对象数组的过滤器管道。过滤器管道具有按服务注入依赖项。服务具有模型数据filterService.data
。
仅在更改服务中的模型时如何激活模板中的管道?
@Pipe({
name: 'filter',
})
export class FilterPipe implements PipeTransform {
constructor(private filterService: FilterService) {}
transform(array: any, start?: any, end?: any): any {}
}
正在使用:
*ngFor="let item of response | filter"
代替签入模板,您可以在服务中保留模型的状态,并仅在满足条件的情况下应用转换。
@Pipe({
name: 'filter',
})
export class FilterPipe implements PipeTransform {
constructor(private filterService: FilterService) {}
transform(array: any, start?: any, end?: any): any {
if (filterService.applyTransform) {
// apply transformation
} else {
return array;
}
}
}