Search code examples
angulartypescriptangular-reactive-forms

How to watch all FormControls ValueChanges except specific control?


I have a form that do a computation whenever a control input value changes.

Here is my form_group looks like:

form_group = this.fb.group({
    control1: [],
    control2: [],
    control3: [],
    ...
    control10: [],
});

I can detect all controls value changes via form_group.valueChanges observable, and do a computation. However i want to exclude some of the control that doesn't need to do such an action.

But there is anyway how to not detect the changes on a particular control?

It is very burdensome to write a form_group.get('controlN').valueChanges and do a computation there. I have 5 or more controls in my form.


Solution

  • You can merge individual valueChanges Observables into one like so :

    Observable.merge(
        [ control1.valueChanges,
          control2.valueChanges ]
      ).subscribe(() => {
        // do your computation
      });