【发布时间】:2017-02-23 06:58:56
【问题描述】:
我对 angular2 中的自定义过滤有疑问。
这是我的场景:
我的页面包含几个自定义组件。其中一个负责在页面左侧显示数据:(componentA)
<md-list-item *ngFor="let item of items | filter : filter | sort: sort; let i = index" " >
<template [render]="itemTemplateRef" [context.item]="item"
[context.index]="index"></template>
</md-list-item>
而且,过滤器是通过自定义过滤器完成的:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({ name: 'filter' })
export class FilterPipe implements PipeTransform {
transform(values: any[], filter: any): any {
if (typeof filter !== "function") return values;
return values.filter((elem) => {
return filter(elem);
});
}
}
在主页中,我将一个函数作为输入属性发送到组件 A:
public filter = (element: MyBean) => {
return (element.email !== undefined);
}
在加载页面时,它的执行没有任何问题。但我想通过单击复选框多次更改过滤条件。 我知道这个事实,如果输入值发生变化,则会执行纯过滤。 但是我不知道,我的场景中的哪个输入值应该更改为强制过滤?
当我在复选框的 Onchange 事件中添加以下功能时,没有任何内容被过滤。(项目绑定到我的组件显示数据中)
createFilterCondition = (searchCondition: string) => {
items.filter((element) => {
return this.doFilter(elem);
});
}
public doFilter= (element: MessagesBean) => {
return (element.email !== undefined);
}
感谢您的帮助
【问题讨论】: