Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge several valueChanges observable dynamic using RXJS operator

I have a dynamic form, something like this.

 profileForm = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
    age: new FormControl(''),
    ...
  });

I want to do something when the value changes, so I did:

this.profileForm.valueChanges.subscribe(() => {
           console.log('changed' + this.profileForm.getRawValue());
});

But I don't want every change from the entire form, just from some of the controls, but I can't do this approach:

this.profileForm.controls['firstName'].valueChanges...

because my form is dynamic. so I tried to create an array in my dynamic method, something like that will return this.

controlsTrigger = ['firstname', 'age'];

note that the last name it's should not trigger the console.log.

so I saw the merge operator from rxjs

merge(
    this.form.get("firstname").valueChanges,
    this.form.get("age").valueChanges,
).subscribe(() => console.log('changed'));

But I want to use it with dynamics controls.

like image 534
paulotarcio Avatar asked Apr 30 '26 11:04

paulotarcio


2 Answers

If your main problem here is, how to get controlsTrigger into merge, you could use Array.map and the spread-operator:

merge(
  ...controlsTrigger.map(name => this.form.get(name).valueChanges))
).subscribe(() => console.log('changed'));
like image 139
churill Avatar answered May 05 '26 14:05

churill


The problem with merge operator is that you can't tell where the emitted value is coming from so you would have to map the valueChanges and create a relation yourself. So something like this should do the trick:

const controls = ["lastName", "firstName"];

merge(
    ...controls.map(c =>
     this.profileForm.get(c).valueChanges.pipe(map(x => ({ control: c, value: x })))
)).subscribe(console.log);

If you use combineLatest it will emit values in the same order as the passed array of observables. BUT it emits only when each control has emitted at least once, which is not optimal if you are listening to formInputs.

Personally I would prefer subscribing to valueChanges of the whole form and execute logic when the fields you are interested in are included, but its up to you

like image 35
Jeff Nikelson Avatar answered May 05 '26 16:05

Jeff Nikelson



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!