简体   繁体   English

patchValue formArray 3 级 Angular 8

[英]patchValue formArray 3 level Angular 8

I have a FormGroup:我有一个表单组:

this.rooms = new FormGroup({
      room:  this._fb.array([
        this._fb.group({
          adultQuantity: [2],
          childQuantity: [0],
          childrenAges: this._fb.array([
            new FormControl('', Validators.required)
          ])
        })
      ])
    });

And data get from API:数据来自 API:

room: [{
    adultQuantity: 2,
    childQuantity: 1,
    childrenAges: [3,5]
},
{
    adultQuantity: 2,
    childQuantity: 2,
    childrenAges: [3,2,9]
}]

How to patch data to FormGroup rooms.如何将数据修补到 FormGroup 房间。
Sorry, I'm not good at English.对不起,我英语不好。
Thanks everyone.感谢大家。

You can patch the form like this,你可以像这样修补表格,

apiResult.map((room: any, index)=>{
(this.rooms.get("room") as FormArray).at(index).patchValue({
          adultQuantity: room.adultQuantity,
          childQuantity:room.childQuantity,
})

  (this.rooms.get("room").controls[index].get("childrenAges") as FormArray).at(index).patchValue({
      childrenAges: room.childrenAges
     })
      
 )}

This might help you https://stackblitz.com/edit/deep-nested-reactive-form这可能会帮助你https://stackblitz.com/edit/deep-nested-reactive-form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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