【发布时间】:2017-10-07 15:17:39
【问题描述】:
我正在开发一个 Angular2 应用程序,我需要为我的一些 *ngFor 指令使用过滤器。
我创建了以下Pipe:
import {Pipe, PipeTransform} from "@angular/core";
@Pipe({
name: 'filter',
pure: false
})
export class FilterPipe implements PipeTransform {
transform(array: Array<Object>, key: string, value: string, negate: boolean): Array<Object> {
if (array === null || array === undefined) {
return [];
} else {
if (negate) {
return array.filter(item => item[key] !== value);
} else {
return array.filter(item => item[key] === value);
}
}
}
}
这是我如何使用它的示例:
<div class="item" *ngFor="let i of items | filter:'type':'S':true">
但如果我要过滤子属性,它就不起作用。 使用 Angular v1.x,我可以应用以下过滤器:
item in array | filter: { type: {subtype: 'foo'}}
您知道如何使用 Angular2 Pipe 达到同样的效果吗? 提前谢谢你。
【问题讨论】:
-
我能问你我的问题有什么问题吗?