【发布时间】:2017-08-16 20:30:56
【问题描述】:
我有一个员工列表,并希望使用预定义的部门过滤器进行下拉
我正在尝试制作一个过滤器管道并将一个函数作为 arg 传递,它仅在第一次渲染时才起作用,但我想在每次用户更改选择时调用该管道
管道:
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
transform(value: Array<any>, f) {
return value.filter(x => f(x));
}
}
组件:
constructor() {
this.filterFunc = this.filterByDepatment.bind(this);
}
//filter function
filterByDepatment(e) {
if (this.selectedDepartment > -1) {
return (e.Departments as Array<any>).find(x => x.Id === this.selectedDepartment);
} else {
return true;
}
}
模板:
<select [(ngModel)]="selectedDepartment">
<option value="-1">All</option>
<option value="{{d.Id}}" *ngFor="let d of departments">{{d.Name}}</option>
</select>
<div class="card" *ngFor="let emp of (employees | filter: filterFunc)">
【问题讨论】:
标签: angular angular-pipe