提交表单并从API接收回调后,如果输入有任何错误,我想使标签文本颜色变为红色,如果没有错误,它将变为绿色。
$('#labelUsername').addClass('danger');
,但如何在离子3中做到这一点? CSS
.danger {
color:red;
}
.success {
color:green;
}
HTML
<ion-item>
<ion-label id="labelCustomer">Customer</ion-label>
<ion-input [(ngModel)]="customer" name="customer" type="text" text-right></ion-input>
</ion-item>
TS
let headers: any = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }),
options: any = { formValue },
url: any = "[urlhere]";
this.http.post(url, options, headers)
.subscribe((data: any) => {
if(data.error){
// $('#labelCustomer').addClass('danger'); <-- something like this
}
},
(error: any) => {
console.log(error);
});
答案 0 :(得分:2)
下面是可用于实现相同代码的代码
<ion-badge [ngClass]="changecolor">Customer</ion-badge>
在component.ts文件中,定义changecolor变量,如:
let headers: any = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }),
options: any = { formValue },
url: any = "[urlhere]";
this.http.post(url, options, headers)
.subscribe((data: any) => {
if(data.error){
//define class name
this.changecolor = "danger"
}else{
this.changecolor = "success"
}
},
(error: any) => {
console.log(error);
});
page-login {
.danger {
color:red;
background-color: #ffffff;
}
.success {
color:green;
background-color: #ffffff;
}
}