1

I am creating an application in angular 2 and typescript.

I want to put some background color in my buttons depending on an variable.

<div>
  <button md-button>1. Choose travel</button>
  <button md-button>2. Choose seats</button>
  <button md-button>3. Fill data</button>
  <button md-button>4. Pay</button>
</div>

I have a variable in my component:

currentStep: number = 1; //1 Select travel, 2 Choose seats, 3 Fill data, 4 Pay

And for example, I want that when the currentStep is equal to 3, the third button change his background color to blue.

Which is the best way to achieve this ?

Thank you.

2
  • you want to apply on click or when? Commented Mar 7, 2017 at 20:09
  • whenever the variable changes Commented Mar 7, 2017 at 21:21

2 Answers 2

2

You can use ngClass:

[ngClass]="{'fill-data-btn': currentStep == 3 }"

In you css:

.fill-data-btn{
background-color: blue;
}

Just to give you an idea.

Sign up to request clarification or add additional context in comments.

Comments

2
<div>
  <button [style.background]=" currentStep === 1 ?'blue':'otherColor'" md-button>1. Choose travel</button>
  <button [style.background]=" currentStep === 2 ?'blue':'otherColor'" md-button>2. Choose seats</button>
  <button [style.background]=" currentStep === 3 ?'blue':'otherColor'" md-button>3. Fill data</button>
  <button [style.background]=" currentStep === 4 ?'blue':'otherColor'" md-button>4. Pay</button>
</div>

Comments

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.