1 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
問題似乎 patchValue 無法填充表單數(shù)組。您可以使用push()像常規(guī)數(shù)組一樣填充表單數(shù)組。
假設(shè)您的 this.qualificationData 是一個(gè)包含許多“expertsQualification”(數(shù)組類型)的數(shù)組,并且類/接口 ExpertsQualification 具有屬性“school”、“l(fā)evel”、“specialization”和“passed_year”,您可以執(zhí)行以下操作:
(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]
});
};
..這樣您就可以循環(huán)遍歷數(shù)組,并為每個(gè)元素將表單組推送到表單數(shù)組。請(qǐng)注意,將其轉(zhuǎn)換為 FormArray 非常重要,例如
(this.educationAndExperienceForm.controls.educationForm as FormArray)
如果你不這樣做,它將保留為 AbstractControl,并且你將無法使用數(shù)組函數(shù),如推、拉等。
添加回答
舉報(bào)