在Angular 2中过滤ngFor循环

时间:2017-04-17 00:15:10

标签: javascript angular

我有这堂课:

    export class Tache {
       id: number;
       text: string;
       stat: number;
}

Stat可以等于0,1或2.

我会在stat = 0时打印 taches ,我尝试使用过滤器

<md-list *ngFor="let tache of taches | filter : 'tache.stat' : '0'"  (click)="onSelect(tache)" [class.selectionnee]="tache === tacheSelectionnee">

但是我收到了这个错误:

zone.js:569 Unhandled Promise rejection: Template parse errors:
The pipe 'filter' could not be found ("

2 个答案:

答案 0 :(得分:3)

您不需要过滤器。通过使用模板包装来分解逻辑。对Angular 4使用ng-template。

   <template *ngFor="let tache of taches">
     <md-list *ngIf="tache.stat==0"
           (click)="onSelect(tache)"
           [class.selectionnee]="tache === tacheSelectionnee">
     </md-list>
   </template>

答案 1 :(得分:2)

名为&#39; filter&#39;的过滤器/管道来自Angular1.x并不存在于Angular2中,这就是你的代码抛出错误的原因。

在这里查看有关Angular2中管道/过滤器如何变化的指南。 https://blog.imaginea.com/pipe-vs-filter-in-angular-js/

如果您愿意,可以为此创建自己的自定义管道,但我同意Dan的说法 - 用模板打破逻辑。

相关问题