【发布时间】:2018-01-25 00:26:43
【问题描述】:
【问题讨论】:
标签: javascript html angular
【问题讨论】:
标签: javascript html angular
我知道的解决方案(hack)是使用setTimeout(function , 0),它会将您的函数发送到事件循环的末尾,直到 angular 完成检查,如下所示:
constructor(private http: HttpClient) {}
ngAfterViewInit() {
setTimeout(() => this.ngAfterViewInitUtil(), 0);
}
ngAfterViewInitUtil(){
this.exampleDatabase = new ExampleHttpDao(this.http);
// The rest of the code
还要注意,这个错误只会在开发模式下发生,不会在生产模式下发生,因为 Angular 不会在生产模式下重新检查应用程序的状态。
您可以在this page 中阅读有关此错误的更多信息
【讨论】:
使用 setTimeout 不是解决此问题的正确方法 你应该使用
ChangeDetectorRef
constructor( private cdr: ChangeDetectorRef){}
ngOnInit(){
this.chu.checkShow.subscribe( value => {
// do something
this.cdr.detectChanges();
});
}
【讨论】: