[Fixed] How to get the name of FormControl which got updated using valueChanges?

Issue

In a form group that is dynamically generated with different FormControl names, valueChanges() is emitting the whole array. How we can identify the exact FormControl name which got updated?

Solution

You can listen to the FormControl itself like this

formGroup.controls.forEach(element => {
   formGroup.get(element).valueChanges = onControlValueChange;
});

onControlValueChange(v){
     // doTheJobHere();
}

You can also follow the same logic for FormArray

formArray.forEach(item => {
  (formArray[item].controls||[]).forEach(element => {
         formArray[item].get(element).valueChanges = onControlValueChange;
     });
});

Leave a Reply

(*) Required, Your email will not be published