我创建了一个管道,其目的是根据标签列表过滤列表。
@Pipe({
name: "tagfilter"
})
export class TagFilterPipe implements PipeTransform {
transform(items: Event[], args: any[]) :any {
if (args.length === 0) {
return items;
}
return _.filter(items, (item: any) => {
return (_.intersection(item.tags, args[0]).length > 0)
});
}
}
然后我像这样使用它:
<tbody class="myline" *ngFor="#event of chapter.events | tagfilter:selectedTags" [event]="event">
但是,“selectedTags”是一个字符串数组,如果我从该数组中添加或删除条目,它不会触发过滤器,因此,我的列表不会被过滤:/
我认为这就是 Angular2 中变化检测的工作原理。我的意思是对象内的更新不会触发更改检测,但如果您更新整个引用,则会触发更改检测。
要重新评估管道,您需要将数组替换为新数组:
@Component({
selector: 'my-app',
template: `
<div>
<span *ngFor="#l of (list | sort:excluded)">{{l}}</span>
</div>
<div (click)="updateArgs()">Update args</div>
`,
pipes: [ SortPipe ]
})
export class AppComponent {
constructor() {
this.list = [ 'n', 'g', 'a', 'u', 'r', 'l' ];
}
updateArgs(array) {
this.excluded = [ 'n', 'g' ];
}
}
在我的示例中,执行时updateArgs
, the transform
再次调用管道的方法。
这是一个笨蛋:https://plnkr.co/edit/GWcEOeY0koXZ5vroRntV?p=preview https://plnkr.co/edit/GWcEOeY0koXZ5vroRntV?p=preview.
希望对你有帮助,
蒂埃里
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)