简体   繁体   English

检查几个值是否以反应形式发生了变化

[英]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, eg phone and address.我的问题是我需要检查两个或多个字段,例如电话和地址。

You can merge observables, but it merges into a single observable, ie您可以合并 observables,但它合并为单个 observable,即

import { merge } from 'rxjs';

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

alternatively you can use combineLatest.或者,您可以使用 combineLatest。 Which will emit the latest value from each observable.它将从每个 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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM