Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 如何根据angular 2中的某些条件更改md卡的背景颜色?_Css_Angular_Material Design_Angular Material2 - Fatal编程技术网

Css 如何根据angular 2中的某些条件更改md卡的背景颜色?

Css 如何根据angular 2中的某些条件更改md卡的背景颜色?,css,angular,material-design,angular-material2,Css,Angular,Material Design,Angular Material2,我通过*ngFor循环动态显示卡片。我想根据某些条件更新卡片背景颜色。例如,如果某些条件满足,则卡片背景颜色为绿色。否则,卡的背景色将保持不变或更改为红色。如何在Angular 2中实现这种动态行为?我尝试使用*ngIf,但后来布局混乱了。我只希望根据条件更改背景颜色,而不影响任何布局更改。对于响应性行为,我使用flex布局 <div fxLayout="row" fxLayoutWrap style="padding-bottom: 25px;

我通过*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]=“条件?'lightBlue':'yellow'”
请尝试以下表达式:
[ngStyle]=“{backgroundColor:data.property=='something'?'green':'red'}”
@Lambo14非常感谢……谢谢worked@Lambo14如何修改上述表达式以使同一属性具有三个条件。例如,data.property>'something'将颜色设置为蓝色,data.property<'something1'将颜色设置为红色,data.property<'something2'将颜色设置为黄色使用三元运算符两次,即[ngStyle]=”{backgroundColor:data.property>'something'?'blue':(data.property<'something1'?'red':'yellow'))