在Angular 6中,可以使用以下方法来格式化[ngClass]中的变量:
在模板中,你可以使用条件表达式来设置[ngClass]的值。例如:
This div has a conditional class
在组件中,你可以定义一个变量,并在需要时更改其值:
export class AppComponent {
isError: boolean = true;
toggleClass() {
this.isError = !this.isError;
}
}
你可以使用字符串插值来动态构建[ngClass]的值。例如:
This div has a dynamic class
在组件中,你可以定义一个变量,并根据需要更改其值:
export class AppComponent {
myVariable: string = '1';
changeClass() {
this.myVariable = '2';
}
}
你可以使用对象字面量来动态设置[ngClass]的值。例如:
This div has dynamic classes
在组件中,你可以定义多个变量,并在需要时更改它们的值:
export class AppComponent {
condition1: boolean = true;
condition2: boolean = false;
toggleClasses() {
this.condition1 = !this.condition1;
this.condition2 = !this.condition2;
}
}
以上是三种在Angular 6中格式化[ngClass]的常见方法。根据你的需求选择合适的方法来格式化[ngClass]中的变量。