angular2 - 动态更改颜色按钮
angular2 - Change color button dynamically
我正在 angular 2 和打字稿中创建一个应用程序。
我想根据变量在我的按钮中添加一些背景颜色。
<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>
我的组件中有一个变量:
currentStep: number = 1; //1 Select travel, 2 Choose seats, 3 Fill data, 4 Pay
例如,我希望当 currentStep 等于 3 时,第三个按钮将他的背景颜色更改为蓝色。
实现此目标的最佳方法是什么?
谢谢。
您可以使用 ngClass
:
[ngClass]="{'fill-data-btn': currentStep == 3 }"
在你里面css:
.fill-data-btn{
background-color: blue;
}
只是给你一个想法。
<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>
我正在 angular 2 和打字稿中创建一个应用程序。
我想根据变量在我的按钮中添加一些背景颜色。
<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>
我的组件中有一个变量:
currentStep: number = 1; //1 Select travel, 2 Choose seats, 3 Fill data, 4 Pay
例如,我希望当 currentStep 等于 3 时,第三个按钮将他的背景颜色更改为蓝色。
实现此目标的最佳方法是什么?
谢谢。
您可以使用 ngClass
:
[ngClass]="{'fill-data-btn': currentStep == 3 }"
在你里面css:
.fill-data-btn{
background-color: blue;
}
只是给你一个想法。
<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>