在我的 ionic 应用程序中,我试图为每个列表项生成不同的颜色,因为我在 comp.ts 中编写了以下函数
getRandomColor() {
var trans = '0.5'; // 50% transparency
var color = 'rgba(';
for (var i = 0; i < 3; i++) {
color += Math.floor(Math.random() * 255) + ',';
}
color += trans + ')'; // add the transparency
return color;
}
Run Code Online (Sandbox Code Playgroud)
在我的 html 文件中,我像下面这样称呼它
<ion-content>
<ion-list class="quoteList" *ngIf = "data" no-lines>
<button ion-item *ngFor="let item of data" [ngStyle]="
{'backgroundColor': getRandomColor() }"
(click)="quoteSelected(item)">
{{ item.name }}
</button>
</ion-list>
</ion-content>
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误,我需要更改以摆脱以下错误
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has …Run Code Online (Sandbox Code Playgroud)