小编Tar*_*rzy的帖子

在 ng for loop angular 4 中更改背景颜色时出错

在我的 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)

ionic2 angular

3
推荐指数
1
解决办法
442
查看次数

标签 统计

angular ×1

ionic2 ×1