I'm testing a method here, for adding tags, I want one of each color, but with the Angle ngFor, the colors are all the same with each new tag added. Here is the code:
<div class="container">
<input type="text" class="form-control" [(ngModel)]="select">
<button type="button" (click)="addItem(select)
(click)="setColor()">Enviar</button>
<div *ngFor="let i of items" class="badge badge-pill">
<div class="badge badge-pill" [ngStyle]="{'background-color' :
randomcolor}">{{ i }}
</div>
</div>
</div>
select: string
items = []
randomcolor: any
addItem(item){
this.items.push(item)
}
getRandomColor(){
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++){
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
setColor(){
this.randomcolor = this.getRandomColor()
}