Angular2:如何应用过滤器* NG-的 [英] Angular2: how to apply filters to *ng-for
问题描述
显然Angular2将在拥堵上使用的管代替过滤器在Angular1与NG-的筛选结果,实现似乎仍然是模糊的,没有清晰的文档。
Apparently Angular2 will use pipe instead of filters as in Angular1 in congestion with ng-for to filter results, the implementation still seems to be vague, with no clear documentation..
即我想要实现可以从以下prespective被视为
Namely what I'm trying to achieve could be viewed from the following prespective
<div *ng-for="#item of itemsList" *ng-if="conditon(item)"></div>
如何实现如此使用管道?
How to implement so using pipes?
推荐答案
基本上,你写一个管道,然后您可以在使用 * NG-对
指令。
Basically, you write a pipe which you can then use in the *ng-for
directive.
在您的组件:
filterargs = {title: 'hello'};
items = [{title: 'hello world'}, {title: 'hello kitty'}, {title: 'foo bar'}];
在您的模板,你可以传递字符串,数字或对象的管道用来过滤的:
In your template, you can pass string, number or object to your pipe to use to filter on:
<li *ng-for="#item of items | myfilter:filterargs">
在您的管道:
@Pipe({
name: 'myfilter',
pure: false
})
@Injectable()
export class MyFilterPipe implements PipeTransform {
transform(items: any[], args: any[]): any {
// filter items array, items which match and return true will be kept, false will be filtered out
return items.filter(item => item.title.indexOf(args[0].title) !== -1);
}
}
诗。我跳过对象类型在本例中为简单起见,但你必须有ID,标题,日期等,你可以在使用的filterExpr对象应用于几项检查与函数的对象。
Ps. I skipped object typing in this example for simplicity, but you would have an object with id, title, date, etc which you could apply several checks on with a function using the filterargs object.
这篇关于Angular2:如何应用过滤器* NG-的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!