Angular反应形式:如何获取刚刚更改的值 [英] Angular Reactive forms : how to get just changed values

查看:51
本文介绍了Angular反应形式:如何获取刚刚更改的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用角度6来构建反应式表单,该表单包含3个属性(名称,年龄,电话),我将只获得更改后的值,而不是所有表单值.

Im building a reactive forms using angular 6, this form contain 3 attributes (name,age,phone) and i would to get just the changed values not all form values.

this.refClientForm = this.formBuilder.group({
  name: [],
  phone: [],
  age: []
});

对于表单侦听器:

 this.refClientForm.valueChanges.subscribe(values => console.log(values))

但是我总是得到所有表单值.

but i got always all form values.

推荐答案

您可以检查所有控件的脏标志.请参见 https://angular.io/api/forms/FormControl

You can check all controls for dirty-flag. See https://angular.io/api/forms/FormControl

getDirtyValues(form: any) {
        let dirtyValues = {};

        Object.keys(form.controls)
            .forEach(key => {
                let currentControl = form.controls[key];

                if (currentControl.dirty) {
                    if (currentControl.controls)
                        dirtyValues[key] = this.getDirtyValues(currentControl);
                    else
                        dirtyValues[key] = currentControl.value;
                }
            });

        return dirtyValues;
}

这篇关于Angular反应形式:如何获取刚刚更改的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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