Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Check if several values have changed in reactive forms

I have some fields in reactive forms that I have to check if values have changed. I know how to check if value have changed on one field like this

this.form.controls['phone'].valueChanges.subscribe(
  data => console.log('form changes', data)
  );

The problem I have is that I need to check two or more fields, e.g. phone and address.

Upvotes: 1

Views: 2026

Answers (1)

Timothy
Timothy

Reputation: 3593

You can merge observables, but it merges into a single observable, i.e.

import { merge } from 'rxjs';

merge(
  this.form.controls['phone'].valueChanges,
  this.form.controls['address'].valueChanges
).subscribe(console.log)

alternatively you can use combineLatest. Which will emit the latest value from each observable. I think this is what you need.

import { combineLatest } from 'rxjs';

combineLatest(
  this.form.controls['phone'].valueChanges, 
  this.form.controls['address'].valueChanges
).subscribe((phone, address) => {
  // do something
})

Upvotes: 8

Related Questions