Angular 2 getting only the dirty values in a controlgroup

后端 未结 3 1397
清酒与你
清酒与你 2021-01-19 06:13

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.

3条回答
  •  时光说笑
    2021-01-19 07:04

    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;
    }
    

提交回复
热议问题