简体   繁体   English

在Angular 2中过滤ngFor循环

[英]Filter ngFor loop in Angular 2

I have this class : 我有这个课:

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

Stat could be equal to 0 , 1 or 2. Stat可以等于0,1或2。

I would print taches when stat = 0, I try to made it with filters 当stat = 0时我会打印taches ,我尝试使用过滤器

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

But I get this error : 但是我得到这个错误:

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

You don't need a filter. 您不需要过滤器。 Break up the logic by wrapping with a template. 通过包装模板来打破逻辑。 Use ng-template for Angular 4. 将ng-template用于Angular 4。

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

The filter/pipe named 'filter' from Angular1.x doesn't exist in Angular2, which is why your code is throwing that error. Angular2中不存在来自Angular1.x的名为“ filter”的过滤器/管道,这就是代码抛出该错误的原因。

Have a look here for a guide on how pipes/filters have changed in Angular2. 在这里查看有关Angular2中管道/过滤器如何更改的指南。 https://blog.imaginea.com/pipe-vs-filter-in-angular-js/ https://blog.imaginea.com/pipe-vs-filter-in-angular-js/

You could create your own custom pipe for this if you wanted to, but I agree with Dan above - break up the logic with a template. 如果愿意,您可以为此创建自己的自定义管道,但是我同意上面的Dan-用模板分解逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM