我写了自己的过滤管,因为它消失在角度2:
@H_502_1@import {Pipe,PipeTransform} from 'angular2/core';
@Pipe({
name: 'myFilter'
})
export class MyFilter implements PipeTransform {
transform(customerData: Array<Object>,args: any[]) {
if (customerData == undefined) {
return;
}
var re = new RegExp(args[0]);
return customerData.filter((item) => re.test(item.customerId));
}
}
并在我的模板中使用它:
@H_502_1@<tr *ngFor="#singleCustomerData of customerData | myFilter:searchTerm"> ... </tr>现在我想看看管道返回多少个匹配项.所以本质上返回数组的大小.
在角度1.x中,我们可以将返回的集合分配给模板中的变量,如下所示:
@H_502_1@<div ng-repeat="person in filtered = (data | filter: query)"> </div>但是我们不能再在角色2中的模板中分配变量.
AFAIK目前没有办法直接做到这一点.一个hack将是向内容添加一个模板变量,并使用ViewChildren(…)查询来获取创建的项目并对其进行计数.
@H_502_1@<tr *ngFor="let singleCustomerData of customerData | myFilter:searchTerm" #someVar>
...
</tr>
<div>count: {{filteredItems?.length}}</div>
@H_502_1@@ViewChildren('someVar') filteredItems;
另一种方法是将对变量的引用传递给管道,如https://plnkr.co/edit/Eqjyt4qdnXezyFvCcGAL?p=preview所示