Search code examples
angularngforangular-ng-if

Angular2 - ngIf in a ngFor


I want to check if the actual element has a value or not.

For Example I want to check if the chocolate is black or white. Depending on this, I want to display the right text.

<ng-container *ngFor="let chocolate of product.getChocolates();">
     <md-card *ngIf="chocolate.getName() == black">IT IS BLACK</md-card>
     <md-card *ngIf="chocolate.getName() == white">IT IS WHITE</md-card>
</ng-container>

How to fix the code, so that it works?


Solution

  • The problem is you missed '(single quote) behind string which is black & white

    <ng-container *ngFor="let chocolate of product.getChocolates();">
         <md-card *ngIf="chocolate.getName() == 'black'">IT IS BLACK</md-card>
         <md-card *ngIf="chocolate.getName() == 'white'">IT IS WHITE</md-card>
    </ng-container>