Angular 2仅获得对照组中的脏值 [英] Angular 2 getting only the dirty values in a controlgroup

查看:72
本文介绍了Angular 2仅获得对照组中的脏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular 2中有一个与自定义ControlGroup一起使用的表单.目前,当我提交表单时,我会将所有带有下面一行的数据传递给我的控制器.

I have a form in Angular 2 that works with a custom ControlGroup. At the moment, when I submit my form, I pass all the data with the following line to my controller.

(ngSubmit)="updateArtist(artistDetailForm.value)"

问题在于,这会传递表单的所有值,如果我的表单很大,这将感觉毫无用处.有可能只传递修改后的(脏的?)值吗?

The problem is that this passes all values of a form, and if my forms are quite large this feels quite useless. Is there a possibility to only pass the modified (dirty?) values?

推荐答案

对Angular Forms进行了新的更改,我对接受的答案做了些微修改,因此可以正常工作.决定分享是否有兴趣. (使用Angular 4 +)

With the new changes to the Angular Forms I've slightly modified the accepted answer, so it works. Decided to share if anyone is interested. (using Angular 4+)

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

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

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

        return dirtyValues;
}

这篇关于Angular 2仅获得对照组中的脏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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