首页 > 解决方案 > 离子3条件下的颜色变化

问题描述

我想根据条件更改颜色。我显示卡片背景必须根据条件更改的 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>

它将在循环结束时更新满足条件的颜色

提前致谢

标签: javascripthtmlcssionic-frameworkionic3

解决方案


  1. NgStyle指令允许您设置给定的 DOM 元素样式属性。

    [ngStyle]="{'background-color':user.userType === 'private' ? 'green' : 'red' }"

  2. NgClass指令允许您为 DOM 元素动态设置 CSS 类。

    [ngClass]="{'text-success':user.userType === 'private'}"

  3. 您也可以创建自己的自定义函数,例如

    [ngStyle]="{'color':getColor(user.userType)}"


推荐阅读