angular点击按钮颜色切换
html文件中
<button [class.color]="flag" (click)="getColor()">点我变变变</button>
<style>
.color {
background-color: skyblue;
}
</style>
ts文件中
export class AppComponent {
flag = false;
getColor(): void {
this.flag = !this.flag;
}
}
<!-- h1标签上有2这个类 color与bg -->
<h1 [ngClass]="{'color' : true,'fs' : true}">
我是H1
</h1>
<style>
.color {
background-color: skyblue;
}
.fs {
font-size: 50px;
}
</style>