3

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)'},
];

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Try another variant. Also I have edited the first one. Try again
2

use ngStyle tag to dynamically add styles

<button *ngFor="let item of arrThemes " [ngStyle]="{'background-color': item.spotColour }"></button>

Comments

1

you can use ngStyle angular directive for that.

like below

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

1 Comment

Please add examples instead of just referring to documentations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.