问题
I have one button, i want to apply a default style of a button and when user click on a button change a button style color to red and background-color to white. Blow is my .css and component.
.btn-default {
color: white;
background-color: blue;
}
.btn-change {
color: Red;
background-color: white;
}
Component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
bntStyle: string;
AppComponent() {
this. bntStyle = 'btn-default';
}
submit() {
this.bntStyle = 'btn-change';
}
.html
<div>
<button name="Save" [ngClass]="['bntStyle']" (onClick)="submit()">Submit</button>
</div>
回答1:
You are binding the string 'btnStyle'. Instead, you should bind the filed:
<div>
<button name="Save" [ngClass]="[bntStyle]" (click)="submit()">Submit</button>
</div>
回答2:
Fire change (OnClick
) to (click
) and then use following code snippet.
<div>
<button name="Save" [ngClass]="[bntStyle]" (click)="submit()">Submit</button>
</div>
DEMO
来源:https://stackoverflow.com/questions/48243620/apply-default-style-and-onclick-change-style-of-a-button-angular-4