Access pipe results in controller or template

2019-09-10 22:35发布

问题:

I have a search pipe which filters a list of objects. I then want to perform an action on all objects that matched that filter. I realize I can import the pipe in my component and rerun the filter but is there any way to access the filtered results themselves?

<input type="text" [(ngModel)]="searchText"/>
<ul>
  <li *ngFor="let item of items | mySearch:searchText">{{item.name}}</li>
</ul>
<button (click)="doActionOnMatchingItems(???)">Do Action</button>

回答1:

Try

*ngFor="let item of filteredItem = (items | mySearch:searchText)"

And then use filteredItem wherever you want


Unlike Angular 1 I'd be happy to aliasing filter result like below. The same has been supported in Angular since version 4. Check NgForOf docs here

*ngFor="let item of items | mySearch:searchText as filteredItem"


标签: angular