如何将过滤器应用于*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
提交
取消
