Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 893
ALSTRA Avatar asked Mar 15 '26 22:03

ALSTRA


1 Answers

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>
like image 180
Pankaj Parkar Avatar answered Mar 18 '26 02:03

Pankaj Parkar