This is my code
home-component.ts
export class HomeComponent implements OnChanges, OnInit, DoCheck, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked {
loading = false;
constructor() { }
ngOnChanges(changes: SimpleChanges): void {
console.log('--parent ngOnChanges--')
}
ngOnInit(): void {
console.log('--parent ngOnInit--')
}
ngDoCheck(): void {
console.log('--parent ngDoCheck--')
}
ngAfterContentInit(): void {
console.log('--parent ngAfterContentInit--')
}
ngAfterContentChecked(): void {
console.log('--parent ngAfterContentChecked--')
}
ngAfterViewInit(): void {
this.loading = true;
console.log('--parent ngAfterViewInit--')
}
ngAfterViewChecked(): void {
console.log('--parent ngAfterViewChecked--')
}
}
home-component.html
<div *ngIf="loading">
<h5>parent component</h5>
</div>
In the code above, I am changing the property value from false to true in the ngAfterViewInt life cycle hook, but my question is why am I getting
ExpressionChangedAfterItHasBeenCheckedError
As per the document ngAfterviewInit is triggered before ngAfterViewChecked, so, Angular has a chance to get the updated value by the time it triggers the ngAfterViewChecked life cycle hook, by no chance I should get this error , then why am I getting this error, please someone guide to the right answer
update 1
This is the order of the life cycle hook
- ngOnChanges
- ngOnInit
- ngDoCheck
- ngAfterViewInit
- ngAfterViewChecked
- ngAfterContentInit
- ngAfterContentChecked
so, after I update the property value in the ngAfterViewInit hook, Angular still has a chance to check for any changes in the ngAfterViewChecked hook, then why is Angular throwing error when the made changes can be found in the ngAfterViewChecked hook