Angular 2 - Can't set form array value

Before calling setValue(data) on a FormGroup or FormArray, the FormGroup or FormArray must have the exactly same structure of the data. Only the structure matters, the value in the FormGroup or FormArray will be replaced by data.

Example, if your data is

let data = [ {a: 1, b:2}, {a: 3, b: 4} ]

Then the FormArray must has 2 FormGroups and each FormGroup must has one FieldControl called a and one called b. You can do something like this:

let fa = this.formBuilder.array([
  this.formBuilder.group({a: 0, b:0}),
  this.formBuilder.group({a: 0, b:0})
])
fa.setValue(data)

If your data is of more complex structure, the FormArray needs to have the same complex structure.


Initialize the form array with the control. That would resolve the issue. See Reactive Forms guide if you need to know how to do that. If you are using ngModel it would probably worth to remove it.