如何將過濾器應用于*ngFor?顯然,Range2將使用管道而不是過濾器(如Angular 1中的過濾器)與ng一起過濾結果,盡管實現似乎仍然是模糊的,沒有明確的文檔。也就是說,我想要達到的目標可以從以下的角度來看<div *ng-for="#item of itemsList" *ng-if="conditon(item)"></div>如何使用管道來實現?
3 回答
qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
*ngFor
filterargs = {title: 'hello'};items = [{title: 'hello world'}, {title: 'hello kitty'}, {title: 'foo bar'}];<li *ngFor="let item of items | myfilter:filterargs">
import { Pipe, PipeTransform } from '@angular/core';@Pipe({
name: 'myfilter',
pure: false})export class MyFilterPipe implements PipeTransform {
transform(items: any[], filter: Object): any {
if (!items || !filter) {
return items;
}
// filter items array, items which match and return true will be
// kept, false will be filtered out
return items.filter(item => item.title.indexOf(filter.title) !== -1);
}}app.module.ts@Component
import { MyFilterPipe } from './shared/pipes/my-filter.pipe';@NgModule({
imports: [
..
],
declarations: [
MyFilterPipe,
],
providers: [
..
],
bootstrap: [AppComponent]})export class AppModule { }添加回答
舉報
0/150
提交
取消
