访问管道结果在控制器或模板中 [英] Access pipe results in controller or template

查看:58
本文介绍了访问管道结果在控制器或模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索管道,用于过滤对象列表.然后,我想对与该过滤器匹配的所有对象执行操作.我知道可以将管道导入组件中并重新运行过滤器,但是有什么方法可以自己访问过滤的结果吗?

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>

推荐答案

尝试

Try

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

然后在任何需要的地方使用 filteredItem

And then use filteredItem wherever you want

与Angular 1不同,我很乐意为如下所示的过滤结果取别名.从版本4开始,Angular一直支持相同的功能.请在 NgForOf docs 此处

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"

这篇关于访问管道结果在控制器或模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆