Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Css From Array Value

I know in Angular you can dynamically add a class to the element but how do I change the value of a property? I want each button to be the next spotColour of the array. I feel like my example explains it well:

<button style="background-color: arrThemes[0]['spotColour']"></button>
<button style="background-color: arrThemes[1]['spotColour']"></button>
<button style="background-color: arrThemes[2]['spotColour']"></button>

arrThemes = [
  {spotColour : 'rgb(238, 68, 136)', baseColour : 'rgb(80, 54, 78)'},
  {spotColour : 'rgb(179, 165, 136)', baseColour : 'rgb(88, 67, 59)'},
  {spotColour : 'rgb(87, 167, 82)', baseColour : 'rgb(47, 68, 46)'},
];
like image 551
Frank Avatar asked Jul 04 '26 02:07

Frank


2 Answers

Try something like this expression with

<button style="{{ 'background-color: ' + arrThemes[0]['spotColour'] }}"></button>

or in more civil way using NgStyle

<button [ngStyle]="{'background-color': arrThemes[0]['spotColour']}"></button>
like image 92
Suren Srapyan Avatar answered Jul 06 '26 15:07

Suren Srapyan


use ngStyle tag to dynamically add styles

<button *ngFor="let item of arrThemes " [ngStyle]="{'background-color': item.spotColour }"></button>
like image 39
Sachila Ranawaka Avatar answered Jul 06 '26 15:07

Sachila Ranawaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!