【发布时间】:2020-03-29 02:01:04
【问题描述】:
我有一个使用类绑定的代码。单击按钮时,字体颜色应根据 textrun 的值更改。 textrun 在 true 和 false 之间变化。如果为 true,它应该以红色显示文本,否则以绿色显示。
TS 文件
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
textrun=true;
messageClasses={
"text-success": !this.textrun,
"text-error": this.textrun,
"text-info": !this.textrun
}
changetrue(){
this.textrun=false;
console.log("done");
}
}
HTML 文件
<h2 [ngClass]="messageClasses">hai</h2>
<button (click)="changetrue()">click</button>
css 文件
.text-success{
color:green;
}
.text-error{
color:red;
}
.text-info{
font-style: italic;
}
编辑:如果要应用多个条件,我需要相同的代码才能工作。
【问题讨论】:
标签: angular