如何观看除特定控件之外的所有 FormControls ValueChanges? [英] How to watch all FormControls ValueChanges except specific control?

查看:22
本文介绍了如何观看除特定控件之外的所有 FormControls ValueChanges?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,每当控件输入值发生变化时都会进行计算.

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

这是我的 form_group 看起来像:

Here is my form_group looks like:

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

我可以通过 form_group.valueChanges observable 检测所有控件值的变化,并进行计算.但是我想排除一些不需要执行此类操作的控件.

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.

但是无论如何如何不检测特定控件上的更改?

编写一个 form_group.get('controlN').valueChanges 并在那里进行计算是非常繁重的.我的表单中有 5 个或更多控件.

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.

推荐答案

您可以像这样将单个 valueChanges Observables 合并为一个:

You can merge individual valueChanges Observables into one like so :

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

这篇关于如何观看除特定控件之外的所有 FormControls ValueChanges?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆