Issue
I have a massive FormGroup, it has many FormControls and many nested FormArrays, and i need to get the value of the top FormGroup except some controls. These controls may be situated very deep in nested FormArrays
Is there any angular way to filter form_group.value or form_group.getRawValue()?
Solution
Looks like there is no built in function for that, so I had to implement some functions that manually collect values:
getFormValue(form_group: FormGroup): any {
const value = {};
Object.keys(form_group.controls).forEach(control_name => {
if (/* condition that shows that the value should not be filtered out */) {
value[control_name] = this.getFormItemValue(form_group.controls[control_name]);
}
});
return value;
}
getFormItemValue(item: AbstractControl): any {
if (item instanceof FormControl) {
return item.value;
} else if (item instanceof FormArray) {
return item.controls.map(form_group => {
return this.getFormValue(form_group as FormGroup);
});
} else if (item instanceof FormGroup) {
return this.getFormValue(item as FormGroup);
}
}
Answered By - lucifer63
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.