我正在尝试在rows
FormArray中显示details
FormArray。我需要帮助,因为我无法显示details
FormArray 的数据。我只知道如何显示rows
FormArray,但details
FormArray对我来说要困难得多。这是我在下面尝试的内容。 请参阅我创建的这个堆栈闪电战文件。
点击此链接
谢谢。
patchValues() {
let rows = this.myForm.get('rows') as FormArray;
this.orders.sales.deliveries.forEach(item => {
rows.push(this.fb.group({
delivery_number: item.delivery_number,
id: item.sales_delivery_id,
details: this.fb.array([this.patchDetails(item.details, item.details.length-1)])
}));
});
}
patchDetails(item_details, i) {
let rows = (<FormArray>this.myForm.controls['rows']).controls[i]['controls']['details'] as FormArray
item_details.forEach(item => {
rows.push(this.fb.group({
sku: item.ingredient_name
}));
});
console.log(rows)
}
details: this.fb.array([this.patchDetails(item.details, item.details.length-1)])
您在创建表单数组时调用patchDetails
,但该方法不返回任何内容,而是可以修改patchDetails
以返回FormArray
,如下所示
patchValues(): void {
let rows = this.myForm.get('rows') as FormArray;
this.orders.sales.deliveries.forEach(item => {
rows.push(this.fb.group({
delivery_number: [item.delivery_number],
id: [item.sales_delivery_id],
details: this.patchDetails(item.details)
}));
});
}
patchDetails(item_details: any[]): FormArray {
let detailsFormArray: FormArray = new FormArray([]);
item_details.forEach(item => detailsFormArray.push(this.fb.group({
sku: [item.ingredient_SKU],
name: [item.ingredient_name]
})));
return detailsFormArray;
}