In Angular
, is there a way to identify which FormGroup
/FormControl
in a dynamicFormArray
emitted the valueChanges
event?
My FormArray
is dynamic. It starts out empty and users could add a FormGroup
to the FormArray
by clicking a button.
When valueChanges, I need to re-validate the control. Since I dont know which control emitted the event, I loop through the entire FormArray
and validate all FormGroup
/FormControl
even though only one control changed - and this is every time when anything in the array changes. How can I avoid doing this?
this.myFormArray
.valueChanges
.subscribe(data => this.onValueChanged(data));
onValueChanged(data?: any): void {
// the data I receive is an entire form array.
// how can I tell which particular item emitted the event,
// so I don’t need to loop through entire array and run validation for all items.
for (let control in this.myFormArray.controls) {
// run validation on each control.
}
}