[英]color change on condition in ionic 3
I want to change the color based on a condition. 我想根据条件更改颜色。 I display the n number of cards the card background must change based on the condition. 我显示n张卡片的数量,卡片背景必须根据条件进行更改。
users.map((item)=>{ if(item.userType == "private"){ document.documentElement.style.setProperty('--customcolor',"yellow"); } else if(item.userType == "private"){ document.documentElement.style.setProperty('--customcolor',"blue"); } });
:root { --customcolor: red; } .oddmes { background-color: var(--customcolor); }
<ion-list><br/> <ion-card class="oddmes" *ngFor="let user of users"> <ion-card-content > <b>{{user.user_name}} </b> <p>{{user.feedback}}</p> </ion-card-content> </ion-card> </ion-list>
it will update the color which satisfy the condition at the end of the loop 它将在循环结束时更新满足条件的颜色
thanks in advance 提前致谢
Simply use : 只需使用:
<div [color]="item.userType == 'private' ? 'primary' : 'secondary'">
Dynamic color
</div>
NgStyle
directive lets you set a given DOM elements style properties. NgStyle
指令使您可以设置给定的DOM元素样式属性。
[ngStyle]="{'background-color':user.userType === 'private' ? 'green' : 'red' }"
NgClass
directive allows you to set the CSS class dynamically for a DOM element. NgClass
指令允许您为DOM元素动态设置CSS类。
[ngClass]="{'text-success':user.userType === 'private'}"
Also you can create your own custom function like 您也可以创建自己的自定义函数,例如
[ngStyle]="{'color':getColor(user.userType)}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.