-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How can the forms plugin update a form array of objects? #1844
Comments
Originally posted by @irowbin in #1745 (comment) on 11 May 2021 It looks bit ugly workaround code but I had to do this way until someone comes up with a better solution to patching FormGroup inside the FormArray Workaround code if someone wondering to have a fix: 😆 Object.keys(this.form.value).forEach((key) => {
const ctrl = this.form.get(key)
const modelValue = model[key]
const isFormArray = this.form.get(key) instanceof FormArray
const isObject =
Array.isArray(modelValue) &&
modelValue.some(
(val) => typeof val === 'object' && !Array.isArray(val)
)
if (isObject && isFormArray) {
const ctrlArray = ctrl as FormArray
// we're going to create new anyway so clear existing first.
// probably need to refactor so we can optimize performance
ctrlArray.clear()
modelValue.forEach((obj) => {
const innerObj = Object.keys(obj)
const formObj = {}
innerObj.forEach((oKey) => {
formObj[oKey] = new FormControl(obj[oKey])
})
ctrlArray.push(new FormGroup(formObj))
})
} else {
ctrl.patchValue(modelValue)
}
}) I see in this line only testing array of string for |
@irowbin, I hope you don't mind, I moved your comments to a new issue, because they were seemingly unrelated to the issue where you posted them. As a side note, I wrote a little utility for a project that creates proxies of FromGroup and FromArray which are able to do exactly what I am talking about here to assist with new values in an array or nested objects that could be set to null and then be set to a value. Here is a stackblitz demo of this utility: https://stackblitz.com/edit/form-array-angular-intercept-patch-v3 |
Originally posted by @irowbin in #1745 (comment) on 10 May 2021
Hi there, I have been struggling to patching the state of
FormArray
values using this plugin.I have something like this in component:
now if I add more than one object in that array, it populates state values as
[{term:'1'}, {term:'2'}]
but it can't be populated from state toFormArray
The structure like this requires to create an
AbstractControl
but couldn't find a way to do from the state to component.example:
Is it done internally or something?
Looks like it was updated using this line which only care about raw value.
Can we have type check and patch value instead of raw?
Thanks,
Regards.
The text was updated successfully, but these errors were encountered: