我试图为具有嵌套属性的模型(FormArray)实现编辑表单。我的语法有问题,而且我不确定自己是否在正确的轨道上。主表单的属性有效,它是我遇到问题的嵌套表单。这是我到目前为止所拥有的。
我在这里发起表单组:
private initForm() {
this.subscription = this.expenseService.getExpense(this.id)
.subscribe(
expense => {
this.expense = expense;
this.patchForm();
}
);
this.expenseEditForm = this.fb.group({
date: '',
amount: '',
check_number: '',
debit: '',
payee_id: '',
notes: '',
expense_expense_categories_attributes:[]
});
}
在这里,我修补表单以设置从我的后端API检索的对象的值。
private patchForm() {
this.expenseEditForm.setValue({
date: '',
amount: this.expense.amount_cents,
check_number: this.expense.check_number,
debit: this.expense.debit,
payee_id: '',
notes: this.expense.notes,
expense_expense_categories_attributes: this.fb.array([
this.setExpenseCategories(),
])
});
}
这就是我被困的地方。如何推送FormArray。如果我试图推送,我会收到一个错误,指出它在FormArray上不存在。
private setExpenseCategories() {
for ( let expenseCategories of this.expense.expense_expense_categories){
this.fb.array.push([
this.fb.group({
expense_category_id: [expenseCategories.expense_category_id, Validators.required],
amount: [expenseCategories.amount_cents]
])
});
}
}
以防万一需要它。这是我的HTML。
<div
*ngFor="let expensecategoriesCtl of expenseEditForm.controls.expense_expense_categories_attributes.controls let i = index"
[formGroupName]="i"
style="margin-top: 10px;">
<md-card>
<md-select class="full-width-input"
placeholder="Expense Category"
id="expense_category_id"
formControlName="expense_category_id"
>
<md-option *ngFor="let expenseCategory of expenseCategories" value="{{expenseCategory.id}}">
{{expenseCategory.category}}
</md-option>
</md-select>
<md-input-container class="full-width-input">
<input
mdInput placeholder="Amount"
type="number"
formControlName="amount">
</md-input-container>
</md-card>
</div>
答案 0 :(得分:12)
对DeborahK的答案进行了一些更改,因为expense.expense_expense_categories
不包含基本类型,而是包含对象。因此,我们无法按原样分配值,但每个对象都需要包装在FormGroup
中,就像您尝试过的那样。
这里我有一个缩短版的代码:
构建表单:
ngOnInit() {
this.expenseEditForm = this.fb.group({
notes: [''],
// notice below change, we need to mark it as an formArray
expense_expense_categories_attributes: this.fb.array([])
})
然后我们在回调中调用patchForm
,就像你一样。该功能看起来像这样,请注意,我们在外面调用this.setExpenseCategories
:
patchForm() {
this.expenseEditForm.patchValue({
notes: this.expense.notes,
})
this.setExpenseCategories()
}
然后是您现有代码的最大变化,我们首先将FormArray
分配给变量control
,然后我们迭代从后端接收的数组,为每个代码创建一个FormGroup
对象并将对象推送到每个FormGroup
:
setExpenseCategories(){
let control = <FormArray>this.expenseEditForm.controls.expense_expense_categories_attributes;
this.expense.expense_expense_categories.forEach(x => {
control.push(this.fb.group(x));
})
}
然后到模板,这个例子没有Angular Material:
<form [formGroup]="expenseEditForm">
<label>Notes: </label>
<input formControlName="notes" /><br>
<!-- Declare formArrayName -->
<div formArrayName="expense_expense_categories_attributes">
<!-- iterate formArray -->
<div *ngFor="let d of expenseEditForm.get('expense_expense_categories_attributes').controls; let i=index">
<!-- Use the index for each formGroup inside the formArray -->
<div [formGroupName]="i">
<label>Amount: </label>
<input formControlName="amount" />
</div>
</div>
</div>
</form>
最后一个
答案 1 :(得分:1)
如果我理解你的问题,你可能需要这样的东西:
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
this.productForm.setControl('tags', this.fb.array(this.product.tags || []));
您可以在此处查看完整示例:APM中的https://github.com/DeborahK/Angular2-ReactiveForms - 更新文件夹。