7

Suppose we have a form that can contain an array of values. For this case Angular provides us with FormArray class.

this.form = new FormGroup({
  addresses: new FormArray([]),
})

Also we have an ability to use NgForm#setValue. This can be used i.e. to populate the form with some data retrieved from server.

When I receive new data from the server, I want to populate the form with that data. For example, my data looks like this:

const data = {
  addresses: [
    { country: 'USA', city: 'New York' },
    { country: 'Germany', city: 'Berlin' },
  ]
}

Now it's time to populate the form with this data. When I try to simply invoke this.form.setValue(data) I get an error:

Cannot set property stack of [object Object] which has only a getter

How do I populate the form with data, but with these requirements:

  1. I know the shape of addresses property.
  2. I don't know the quantity of addresses in that data.addresses array.
  • Did you found a solution to this? the problem here i think is the quantity of items – kaseOga Oct 4 '16 at 15:51
  • @kaseOga yes, exactly! But the problem is not solved for me. I don't know how to describe a generic array interface and apply it to dynamic list of elements – Girafa Oct 5 '16 at 6:05
  • @Girafa. Do you still need help with this question? – AngularChef Feb 23 '17 at 14:50
  • @AngularChef yes please – Augie Gardner Jun 5 '18 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.