如果更改了模型,如何在Angular中激活管道?

问题描述 投票:0回答:1

我有用于过滤对象数组的过滤器管道。过滤器管道具有按服务注入依赖项。服务具有模型数据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"
angular
1个回答
0
投票

代替签入模板,您可以在服务中保留模型的状态,并仅在满足条件的情况下应用转换。

@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;
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.