How to fix error when assigning to random color in array?


#1

I want to assign to a random color of the array. It actually first works but then I get an error message: “ExpressionChangedAfterItHasBeenChecked” I even see how it one second before the message come the color of the chip change very fast. So it somehiw works… How can I fix this issue. (I also added the code and funcs for the createion of the chips)

HTML

<ion-chip [color]="color[getRandomInt(color.length)]" class="chip" #chip *ngFor="let tag of tagName">

TS

export class Tag {
  tag: string;
  constructor(values: Object = {}) {
    Object.assign(this, values);
  }
...
color: string [] = ["ok", "nice","awesome","danger","white"]

 tagName: Tag[] = [];
...
add(): void {
  let id = this.tagName.length + 1;
  this.tagName.push(new Tag({ tag: "tag" + id }, ));
}

remove(tag: Tag) {
  let id = this.tagName.indexOf(tag);
  this.tagName.splice(id, 1);
}


getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}