在Angular 6中,可以使用以下方法来解决不调用ngClass的问题:
FormsModule
或ReactiveFormsModule
,并将其添加到@NgModule
装饰器的imports
数组中。import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
],
// other configurations
})
export class AppModule { }
ngClass
指令,并且表达式返回布尔值。Example
其中,condition1
和condition2
是在组件中定义的布尔类型变量。
ngClass
表达式的条件,确保在更改后调用了ChangeDetectorRef
的detectChanges()
方法。import { Component, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-example',
template: `
Example
`
})
export class ExampleComponent {
condition1: boolean = true;
condition2: boolean = false;
constructor(private cdr: ChangeDetectorRef) { }
changeConditions() {
this.condition1 = !this.condition1;
this.condition2 = !this.condition2;
this.cdr.detectChanges();
}
}
在上述示例中,当点击按钮时,会切换condition1
和condition2
的值,并调用detectChanges()
方法来更新视图。
如果以上方法都没有解决问题,可以尝试重启Angular开发服务器或清除浏览器缓存,有时候这些问题可能是由于开发环境或缓存问题引起的。