Angular2 - 在ngFor中的ngIf

ALS*_*TRA 3 angular-ng-if ngfor angular

我想检查实际元素是否有值.

例如,我想检查巧克力是黑色还是白色.根据这个,我想显示正确的文本.

<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>
Run Code Online (Sandbox Code Playgroud)

如何修复代码,以便它可以工作?

Pan*_*kar 8

问题是你错过了'(单引号)背后string是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>
Run Code Online (Sandbox Code Playgroud)