答案 0 :(得分:7)
<强> DEMO 强>
如果你想通过其他字段过滤,只需将它们添加到管道中:
import { Pipe,PipeTransform} from '@angular/core';
@Pipe({
name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
transform(items: any[], criteria: any): any {
return items.filter(item =>{
for (let key in item ) {
if((""+item[key]).toLocaleLowerCase().includes(criteria.toLocaleLowerCase())){
return true;
}
}
return false;
});
}
}
@Component({
selector: 'sites-component',
template: `
<input #search (keyup)="0">
<ul>
<li *ngFor="let site of (sites | searchFilter: search.value )">(...)</li>
</ul>
`
})
export class SitesComponent{
sites : Array;
}
不要忘记在模块中声明管道。