ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'
you have to tell angular
that you updated the content after ngAfterContentChecked
you can import ChangeDetectorRef
from @angular/core
and call detectChanges
import {ChangeDetectorRef } from '@angular/core';
constructor( private cdref: ChangeDetectorRef ) {}
ngAfterContentChecked() {
this.sampleViewModel.DataContext = this.DataContext;
this.sampleViewModel.Position = this.Position;
this.cdref.detectChanges();
}
The ngAfterContentChecked
lifecycle hook is triggered when bindings updates for the child components/directives have been already been finished. But you're updating the property that is used as a binding input for the ngClass
directive. That is the problem. When Angular runs validation stage it detects that there's a pending update to the properties and throws the error.
To understand the error better, read these two articles:
- Everything you need to know about the
ExpressionChangedAfterItHasBeenCheckedError
error - Everything you need to know about change detection in Angular
Think about why you need to change the property in the ngAfterViewInit
lifecycle hook. Any other lifecycle that is triggered before ngAfterViewInit/Checked
will work, for example ngOnInit
or ngDoCheck
or ngAfterContentChecked
.
So to fix it move renderWidgetInsideWidgetContainer
to the ngOnInit()
lifecycle hook.
If you are using <ng-content>
with *ngIf
you are bound to fall into this loop.
Only way out I found was to change *ngIf
to display:none
functionality