1 回答

TA貢獻1943條經驗 獲得超7個贊
問題似乎 patchValue 無法填充表單數組。您可以使用push()像常規數組一樣填充表單數組。
假設您的 this.qualificationData 是一個包含許多“expertsQualification”(數組類型)的數組,并且類/接口 ExpertsQualification 具有屬性“school”、“level”、“specialization”和“passed_year”,您可以執行以下操作:
(this.educationAndExperienceForm.controls.educationForm as FormArray).reset(); //reset the FormArray
this.qualificationData.forEach(x => {
(this.educationAndExperienceForm.controls.educationForm as FormArray)
.push(this.fb.group({
school: [x.school, Validators.required],
degree: [x.degree, Validators.required],
specialization: [x.specialization, Validators.required],
passed_year: [x.passed_year, Validators.required]
});
};
..這樣您就可以循環遍歷數組,并為每個元素將表單組推送到表單數組。請注意,將其轉換為 FormArray 非常重要,例如
(this.educationAndExperienceForm.controls.educationForm as FormArray)
如果你不這樣做,它將保留為 AbstractControl,并且你將無法使用數組函數,如推、拉等。
添加回答
舉報