【发布时间】:2021-09-22 23:09:38
【问题描述】:
晚安各位, 我在填充表单数组时遇到以下问题,我设法将代码开发到一个步骤,但现在我陷入了一个步骤。 我设法使用 setControl 填充数组,但现在我需要在这个数组中填充一个数组,我遇到了很多麻烦。 我能够填充原料数组,但无法填充 feedstockOptions 数组 按照下面的代码:
Method populate form
if (response.feedstocks?.length >=1 && response.feedstocks != null) {
this.feedstockForm.setControl('feedstock', this.editFeedstock(response.feedstocks))
}
editFeedstock(response) {
const formArray = new FormArray ([]);
response.forEach (s => {
formArray.push ( this.fb.group ({
category: s.category,
position: s.position,
feedstockOptions: s.feedstockOptions (//this.editFormOptions(s.feedstockOptions))
}));
});
return formArray;
}
editFormOptions(response) {
const array = new FormArray([]);
response.forEach(r => {
array.push(this.fb.group({
feedstockOptions: r.feedstockOptions
}))
})
console.log(response)
return array
}
Form
feedstockForm: FormGroup = this.fb.group({
feedstock: this.fb.array([
this.feedstockFormsCategory()
])
});
feedstockFormsCategory(): FormGroup {
return this.fb.group({
category: "",
position: "",
feedstockOptions: this.fb.array([
this.feedstockFormsComponent()
])
})
};
feedstockFormsComponent(): FormGroup {
return this.fb.group({
name: '',
code: '',
price: this.fb.group({
amount: ['', Validators.required],
currency: ['BRL'],
}),
})
};
【问题讨论】:
标签: angular typescript forms angular-reactive-forms formarray