[英]Angular change detection error: ExpressionChangedAfterItHasBeenCheckedError
[英]Angular4 Change Detection ExpressionChangedAfterItHasBeenCheckedError
我正在尝试解决一个问题,由于$digest
-cycle检查了所有内容,因此该问题在AngularJS中不存在。
我正在尝试创建一个可以初始化自身(异步)并通知其周围的容器加载已完成的组件。
我有这个伪表:
<my-table>
<ng-container *ngFor="let row in rows">
<my-table-row
[row]="row"
[isLoading]="row.isLoading"
(click)="onClickRow(row)"
></my-table-row>
<my-detail-row *ngIf="row.isExpanded">
<my-component
[data]="row"
></my-component>
></my-detail-row>
</ng-container>
</my-table>
在周围的组件(保存rows
)中,我具有onClickRow(row)
函数,用于切换row.isExpanded
。
在my-component
组件中,我想将row.isLoading
设置为true(通知“父”行它正在加载),然后在API调用完成后将其设置为false。
@Input() data: any;
ngOnInit() {
this.task.isLoading = true; // PROBLEM
setTimeout(() => { // simulate API call
this.task.isLoading = false; // no problem
}, 2000);
}
现在,我收到此错误: Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'true'.
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'true'.
我猜这是因为更改是从my-component
到树到ng-container
,然后才是my-table-row
?
我有一个解决方法,可以在this.task.isLoading
的第一个设置周围使用第二个setTimeout()
,但这会带来一些弹出效果,如果可能的话,我想找到一个更干净的解决方案。
有没有人有建议,这怎么行?
我找到了可以接受的解决方案。
您可以将“父级”注入到组件中(类似于AngularJS中的require
)。
为此,您需要将my-table-row
和my-detail-row
合并为my-item-row
。 这具有额外的好处,您可以将切换逻辑放入组件中,而不必在每次使用时都重新实现它。
my-item-row.component.html
:
<div
class="item-row"
(click)="onToggleDetail()"
>
<div class="cell">...</div>
<div *ngIf="isLoading" class="loading"></div>
</div>
<div
*ngIf="isExpanded"
[hidden]="!isInitialized"
class="detail-row"
>
...
</div>
my-item-row.component.ts
:
import { Component } from '@angular/core';
import { Input, ChangeDetectorRef } from '@angular/core';
@Component({...})
export class MyItemRowComponent {
...
isInitialized: boolean = false;
isLoading: boolean = false;
isExpanded: boolean = false;
constructor(private changeDetector: ChangeDetectorRef) { }
onToggleDetail() : void {
this.isExpanded = !this.isExpanded;
}
public startLoading() : void {
this.isLoading = true;
this.isInitialized = false;
this.changeDetector.detectChanges(); // trigger re-evaluate
}
public stopLoading() : void {
this.isLoading = false;
this.isInitialized = true;
this.changeDetector.detectChanges(): // trigger re-evaluate
}
}
现在,您可以将其注入my-component
,如下所示:
my-component.component.ts
:
import { Component } from '@angular/core';
import { Input, Host, Optional } from '@angular/core';
import { MyItemRowComponent } from '...';
@Component({...})
export class MyComponentComponent {
...
// Optional, because there may be a case where we want to use it outside of a row
constructor(@Optional() @Host() private parent: MyItemRowComponent) { }
ngOnInit() { // or ngAfterViewInit, doesn't matter
if (this.parent) {
this.parent.startLoading();
}
setTimeout(() => { // simulate API call
if (this.parent) {
this.parent.stopLoading();
}
}, 2000);
}
}
这是我目前的解决方案。
这导致启动my-component
的另一个问题,即使尚未扩展,也请参见此处:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.