user6196015
user6196015

Reputation:

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.

I would print taches when stat = 0, I try to made it with filters

<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 ("

Upvotes: 2

Views: 2271

Answers (2)

Stephen R. Smith
Stephen R. Smith

Reputation: 3400

The filter/pipe named 'filter' from Angular1.x doesn't exist in Angular2, which is why your code is throwing that error.

Have a look here for a guide on how pipes/filters have changed in Angular2. 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.

Upvotes: 2

Dan
Dan

Reputation: 1355

You don't need a filter. Break up the logic by wrapping with a template. Use ng-template for Angular 4.

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

Upvotes: 3

Related Questions