我正在编辑页面,我试图用从API获得的数据设置表单的默认值.
我的问题是,我的数组中有两个项目,但它会创建另一个空的formArray;
我做错了什么?
这是我的stackblitz美元
this.filterForm = this.fb.group({
categories: this.fb.array([this.initCat()]),
});
const control = <FormArray>this.filterForm.get('categories');
console.log(control);
this.chosenCarPartCategories.carPartCategories.forEach((x) => {
control.push(
this.patchValues(x.name, x.carPartSubCategoryId, x.quantity, x.comment)
);
});
patchValues(name, carPartSubCategoryId, quantity, comment) {
return this.fb.group({
carPartCategory: [name],
carPartSubCategory: [carPartSubCategoryId],
quantity: [quantity],
comment: [comment],
});
}
initCat() {
return this.fb.group({
carPartCategory: ['', Validators.required],
carPartSubCategory: ['', Validators.required],
quantity: ['', Validators.required],
comment: [''],
});
}