簡體   English   中英

如何將對象數組轉換為表單組數組?

[英]How do i turn an object array into a formgroup array?

我想使用formbuilder的array函數來使用formcontrol數組。 我有一個現有的自定義對象數組,需要添加到該FormArray但是它僅將FormGroup作為參數。 如何將自定義對象數組轉換為FormGroup對象,以便可以將其添加到FormArray中? 我已經嘗試過了:

let formGroupArray: FormGroup[] = [];
    for(let i=0; i < account.laborperiods.length; i++){
      let formGroup = this.fb.group({
        id: this.account.laborperiods[i].id,
        beginDate: this.account.laborperiods[i].beginDate,
        endDate: this.account.laborperiods[i].endDate,
        hours: this.account.laborperiods[i].hours,
        account: this.account.laborperiods[i].account
      })
        formGroupArray.push(formGroup)
    }
    this.laborPeriodArray = new FormArray(formGroupArray);

但這給了我錯誤"ERROR TypeError: Circular reference in value argument not supported"

那么誰能告訴我如何將自定義對象數組轉換為FormGroup數組? 還是有一種更方便的方法可以將自定義對象數組直接轉換為FormArray?

嘗試:

let formGroupArray:FormArray
for(let i=0; i < account.laborperiods.length; i++){
      let formGroup = this.fb.group({
          ...
      })
      if (!formGroupArray)
        formGroupArray=new FormArray(formGroup);
      else
        formGroupArray.push(formGroup)
}
//Change this line
this.laborPeriodArray = formGroupArray;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM