0

I have a FormArray inside a form and the FormArray has a string parameter called "Foo". I tried to do:

let formArray = this.form.get("Foo") as FormArray;
let formArrayValues : {Foo : string}[];  //this will be put into the ts model to be sent out.

I am not sure how i can iterate through the FormArray, get the value "Foo" and put it into my new array. I tried :

for (let c of formArray.controls) {
   formArrayValues.push(c.get("Foo"));
}

but it seems i get error saying "Foo" is missing in type in AbstractControl. I am a bit lost here and I did search around but didn't find anything similar. Thanks

1
  • Instead of c.get("Foo") you can read the value of a control with c.value Commented Dec 21, 2021 at 16:08

1 Answer 1

1

You don't need to iterate the form array, you can simply access the FormArray's value like this.

formArrayValues = [...formArray.value];

I suggest reading the original documentation to better understand what functionality is available for you and how to utilize form array to its max, just to avoid reinventing the wheel scenarios like this example (for example trying to iterate the form array control just to get the value when value is already available to you)

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. i don't seem to have any error setting the result to the model so will need to test further. I did read the documentation you pointed but maybe because I am still bit new to the typescript syntax and the "Form" in general, i dont think I saw any reference in that document with your answer of [...formArray.value]
The reference in the document mentions "value" as a property you can access for form array, the triple dots is called spread syntax, used to copy arrays, you can google spread syntax and read more about it, I hope this helped you.
thanks! Yeah i did some testing and it seems the data is being passed to the model and in the rest call :). Not sure the model itself is right BUT the hard part of getting the data from screen to call is complete. Thanks :).