Angular 2 只获取控制组中的脏值 [英] Angular 2 getting only the dirty values in a controlgroup
问题描述
我在 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屋!