I'm trying to solve a problem, that didn't exist in AngularJS, due to the $digest
-cycle checking everything.
I'm trying to create a component, that can initialize itself (async) and notify its surrounding container that the loading has been completed.
I have this pseudo-table:
<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>
In the surrounding component (that holds the rows
), I have the function onClickRow(row)
which toggles row.isExpanded
.
In the my-component
component I want to set row.isLoading
to true (notifying the "parent" row that it is loading) and then setting it to false after the API call is completed.
@Input() data: any;
ngOnInit() {
this.task.isLoading = true; // PROBLEM
setTimeout(() => { // simulate API call
this.task.isLoading = false; // no problem
}, 2000);
}
Now I get this error: Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'true'.
I guess this is because the change goes from my-component
up the tree to ng-container
but then not down to my-table-row
?
I have a workaround, in that I can use a second setTimeout()
, around the first setting of this.task.isLoading
, but this leads to some popin-effects and I would like to find a cleaner solution if possible.
Does anyone have a suggestion, how this can work?