Angular 2/4 Edit Form填充FormArray控件 [英] Angular 2/4 Edit Form Populate FormArray Controls
问题描述
我正在尝试为具有嵌套属性(FormArray)的模型实现编辑表单。我在语法上遇到麻烦,不确定是否在正确的轨道上。主表单的属性起作用,这是我遇到问题的嵌套表单。
I'm trying to implement an edit form for a model with nested attributes (FormArray). I'm having trouble with the syntax and I'm uncertain whether I'm on the right track. The attributes for the main form work, it's the nested form I'm having problems with. Here's what I have so far.
在这里我启动表单组:
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中检索的对象的值
Here I patch the form to set values from an object retrieved from my backend 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上不存在。
This is where I'm stuck. How do I push onto a FormArray. If I try to push, I get an error stating that push it doesn't exist on 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。
Just in case it's needed. Here's my 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>
推荐答案
由于<$ c $,DeborahK的答案有些变化c> expense.expense_expense_categories 不包含基本类型,但包含对象。因此,我们不能按原样分配值,但是每个对象都需要包装在 FormGroup
中,就像您尝试过的那样。
Some changes to DeborahK's answer, since expense.expense_expense_categories
doesn't contain a primitive types, but objects. Therefore we cannot assign the values as is, but each object needs to be wrapped in a FormGroup
, just like you have attempted.
这是我的代码的简化版本:
Here I have a shortened version of your code:
构建表单:
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
:
Then we call patchForm
in the callback, just like you have. That function would look like this, notice, we call this.setExpenseCategories
outside:
patchForm() {
this.expenseEditForm.patchValue({
notes: this.expense.notes,
})
this.setExpenseCategories()
}
然后是您现有代码中最大的变化,我们首先在其中分配 FormArray
到变量 control
,然后我们迭代从后端收到的数组,创建一个每个对象的FormGroup
并将对象推送到每个 FormGroup
:
Then comes the biggest change from your existing code, where we first assign the FormArray
to the variable control
and then we iterate your array received from backend, create a FormGroup
for each object and push the object to each 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:
Then to the template, this example is without 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>
最后a
这篇关于Angular 2/4 Edit Form填充FormArray控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!