简体   繁体   中英

Angular 2 patchValue inserts only first object of nested array

I have a form in a model driven approach. In this form there is a nested Array like this:

ngOnInit() {
  this.reproOrderForm = this.formBuilder.group({
    ...
    anyArray: this.formBuilder.array([
      this.formBuilder.group({
        anyValue: []
      })
    ])
  });
}

When I have an object and make patchValue on this form only the first object ist filled into the form.

{
    anyArray:[
         {anyValue:"test"},
         {anyValue:"asdf"}
    ]
}

So in this case only {anyValue:"test"} is available in my form after patchValue. Is this a known bug or even on purpose? It doesn´t make sense to me.

You can´t use patchValue for populating an array in your formgroup. Instead you have to push the values onto the formgroup array by using a for-loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM