Angular 2+ - check if Pipe returns an empty subset of original list

前端 未结 7 1353
天涯浪人
天涯浪人 2021-02-04 02:37

I have a list of strings that I want to iterate through, but I want to be able to filter them using a search term. Like this:

7条回答
  •  滥情空心
    2021-02-04 03:10

    It's possible to leverage dependency injection into pipes. You could inject the component:

    Then you can set a property on it to notify this:

    @Pipe({
      name: 'search'
    })
    export class SearchPipe {
      constructor(@Inject(forwardRef(() => SomeComponent)) private comp:SomeComponent) {
    
      }
    
      transform(value) {
        var filtered = value.map((v) => v-1);
        this.comp.isEmpty = (filtered.length === 0);
        return filtered;
      }
    }
    

    The main drawback is that you link the pipe within the component. The advantage is that filtering is executed once.

提交回复
热议问题