I am seeing Expression Has Changed error with below code which works fine in V11 but fails in V14.
Its a simple code , where i am creating a form Group in parent component and passing it to Child Component where i am adding a form control ( with required validator ), which will make the form group invalid.
app.component.html :
import { Component } from '@angular/core';
import {FormGroup} from "@angular/forms";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'helloworld';
formGroup: FormGroup = new FormGroup<any>({});
}
app.component.html :
<form [formGroup]="formGroup">
Form Is Valid : {{formGroup.valid}}
{{title}}
<app-child [formGroup]="formGroup"></app-child>
</form>
child.component.ts :
import {Component, Input, OnInit} from '@angular/core';
import {FormControl, FormGroup, Validators} from "@angular/forms";
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {
@Input() formGroup: FormGroup;
constructor() { }
ngOnInit(): void {
this.formGroup.addControl('name', new FormControl('', [Validators.required]));
console.log('ChildComponent OnInit')
}
}
child.component.html :
<ng-container [formGroup]="formGroup">
<input formControlName="name">
</ng-container>
It is complaining with below error ( on line Form Is Valid : {{formGroup.valid}}
in app.component.html)
ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'
My understanding was , app component will update its bindings( DOM Update/ Rendering ) only after child component OnInit /DoCheck hooks run.