在Angular 7中,如果ngIf在第一次之后不起作用,可能是因为变化检测机制的原因。以下是一些可能的解决方法:
detectChanges()
方法来手动触发变化检测机制。例如:import { Component, ChangeDetectorRef } from '@angular/core';
@Component({
// ...
})
export class MyComponent {
constructor(private cdr: ChangeDetectorRef) {}
// ...
ngAfterViewInit() {
this.cdr.detectChanges();
}
}
import { Component, ChangeDetectionStrategy } from '@angular/core';
@Component({
// ...
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComponent {
// ...
}
这些解决方法中的任何一种都可能解决ngIf在第一次之后不起作用的问题。根据具体情况选择适合您的解决方法。
上一篇:Angular 7中的“没有名为error的表单控件的值访问器”
下一篇:Angular 7中的“Routing URL is not working as previous”可以翻译为“路由URL在Angular 7中不像以前那样工作”。