我通过遍历 *ngFor 来动态显示卡片。我想根据某些条件更新卡片背景颜色。例如如果满足某些条件,则卡片背景为绿色。否则,卡片背景颜色不变或更改为红色。如何在 Angular 2 中实现这种动态行为?我尝试使用 *ngIf,但随后布局变得一团糟。我只想根据条件更改背景颜色,而不影响任何布局更改。对于响应式行为,我使用的是 flex 布局。
<div fxLayout="row" fxLayoutWrap style="padding-bottom: 25px;
padding-top: 25px;
margin: auto;
justify-content: center">
<md-card fxFlex.gt-md="45"
fxFlex.md="45"
fxFlex.sm="auto"
fxFlex.xs="100"
*ngFor="let data of myArray"
[style.background]="'lightBlue'"
style="margin:5px;">
<md-card-content>
<h1></h1>
<h2></h2>
<h2></h2>
</md-card-content>
</md-card>
</div>
最佳答案
这也很有效 [style.background]="condition ? 'lightBlue':'yellow'"
关于css - 如何根据 Angular 2 中的某些条件更改 md 卡的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44019481/