Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - ngOnChange on FormGroup

I need to detect real time changes to the fields of a FormGroup. I have a simple parent-child components structure as follows:

parent component view

<my-child [myForm]="myForm"></my-child>

child component controller

@Input()
myForm: FormGroup;

ngOnChanges(changes: SimpleChanges) {
console.log('changes', changes)
}

The problem is that no change is detected when the form fields change. Is there a way to trigger the ngOnChanges to any form change? Thanks.

like image 419
esseara Avatar asked Jan 23 '26 09:01

esseara


1 Answers

You can use the following:

this.myForm.valueChanges.subscribe(changes => {
    // do what you need with the form fields here
    // you can access a form field via changes.fieldName
});
like image 180
Hung Tran Avatar answered Jan 26 '26 02:01

Hung Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!