首页 > 解决方案 > 我可以在 Ionic 4 中动态更改 ion-item 自定义 css 属性 --background 吗?

问题描述

我们如何设置动态背景颜色?我需要基于数组渲染 ion-item 元素。我需要在 ngFor 循环中设置背景颜色,来自数组中的每个值。有任何想法吗?

我试过这样的事情。但它不起作用

<ion-list *ngFor="let shift of shifts">
     <ion-item [style.--background]="'shift.color'">{{ shift.color }} qwe</ion-item>
</ion-list>

标签: angularionic-frameworkionic4

解决方案


尝试这个,

<ion-list *ngFor="let shift of shifts">
     <ion-item [style.background]="shift.color"> {{shift.color}} qwe</ion-item>
</ion-list>

推荐阅读