How to listen to multiple form fields on change in Angular?

Viewed 1418

Scenario

My form has 8 fields but I want to listen changes on three form fields to apply same function to them.

I dont want to do this:

this.headerForm.get('start').valueChanges.subscribe(() => applyFunction());
this.headerForm.get('end').valueChanges.subscribe(() => applyFunction());
this.headerForm.get('middle').valueChanges.subscribe(() => applyFunction());

Im using this:

merge([
     this.headerForm.get('start').valueChanges,
     this.headerForm.get('end').valueChanges,
     this.headerForm.get('middle').valueChanges
]).subscribe(value => console.log(value));

But its not working, it shows me something like this when I start app, and when I make any change on any of those fields it doesnt show me nothing on console:

enter image description here

1 Answers

Solution:

Remove [] from merge function.

merge(
     this.headerForm.get('start').valueChanges,
     this.headerForm.get('end').valueChanges,
     this.headerForm.get('middle').valueChanges
).subscribe(value => console.log(value));
Related