【发布时间】:2019-05-02 22:35:19
【问题描述】:
我有一个 FormGroup,它有三个 FormControl 字段和一个 FormArray 字段,如下图所示。要求是从用户那里获取经理姓名,一旦按下添加按钮,经理详细信息应显示在表格中。在表格中提供了一个删除按钮,当按下删除按钮时,管理器应该从表格和列表中删除。提交表单时,应保存管理员列表。除了 formArray 逻辑之外,一切正常。
我试图在网上找到解决方案(点击各种链接:-https://alligator.io/angular/reactive-forms-formarray-dynamic-fields/, Angular 4 Form FormArray Add a Button to add or delete a form input row),但没有多大帮助。关于如何在formGroup中存储formArray的资料不多。请提出建议。
下面是我的代码,请看一下:-
1. manager-create-modal.component.html
<div>
<form [formGroup]="createForm" (ngSubmit)="onFormCreation()">
<div class="row">
<div class="column">
<div class="form-inline">
<div class="form-group">
<label for="remote_access_method">Remote Access Method: <font color="orange"> *</font> </label>
<input type="text" size='38' class="form-control" formControlName="remote_access_method" >
</div>
</div>
<br>
<div class="form-inline">
<div class="form-group">
<label for="status">Current Status: <font color="orange"> *</font> </label>
<input type="text" size='38' class="form-control" formControlName="status">
</div>
</div>
<br>
<div class="form-inline">
<div class="form-group">
<label for="secregid">Registration ID:<font color="orange"> *</font> </label>
<input type="text" size='38' class="form-control" formControlName="secregid">
</div>
</div>
<br><br>
<div class="form-inline">
<div class="form-group">
<br><br>
<div formArrayName="manager_formArray">
Enter name: <input type="text" class="form-control" formControlName="MgrName" size='50' >
<button type="button" class="btn btn-primary btn-sm" (click)="addPM()">Add</button>
<br><br>
<table class="table table-hover">
<tr><th>#</th><th>Manager Name</th><th>Remove</th></tr>
<tr *ngFor="let pm of createForm.get('manager_formArray').value; let id = index">
<td>{{id+1}}</td>
<td>{{pm.MgrName}}</td>
<td>
<span class="table-remove">
<button type="button" class="btn btn-primary btn-sm" (click)="removeMgr(id)">Remove</button>
</span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<br>
</div>
<br><br>
<div class="form-group">
<button class="btn btn-primary">Submit</button>
</div>
</form>
</div>
2。 manager-create-modal.component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormArray, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app-manager-create-modal',
templateUrl: './manager-create-modal.component.html',
styleUrls: ['./manager-create-modal.component.css']
})
export class ManagerCreateModalComponent implements OnInit {
createForm: FormGroup;
manager_formArray: FormArray;
remote_access_method: FormControl;
status: FormControl;
secregid: FormControl;
constructor(private formBuilder: FormBuilder) { }
createFormControls(){
this.remote_access_method = new FormControl('');
this.status = new FormControl('');
this.secregid = new FormControl('');
this.manager_formArray = new FormArray([ this.createItem() ]);
}
createItem(): FormGroup {
return this.formBuilder.group({
MgrName: ''
});
}
createFormVariables(){
this.createForm = new FormGroup({
remote_access_method : this.remote_access_method,
status : this.status,
secregid : this.secregid,
manager_formArray: this.manager_formArray,
})
}
ngOnInit() {
this.createFormControls();
this.createFormVariables();
}
addPM(mgr: any): void {
console.log("inside addPM");
this.manager_formArray.push(this.formBuilder.group({MgrName:''}));
console.log("list after addition:"+this.manager_formArray.value);
for(let i = 0; i < this.manager_formArray.length; i++) {
console.log(this.manager_formArray.at(i).value);
}
}
get managerFormArray() {
return this.manager_formArray.get('MgrName') as FormArray;
}
onFormCreation(){
console.log("success")
}
}
经理姓名未显示在表格中,我不断收到以下错误:-
错误错误:找不到带有路径的控件:'manager_formArray -> MgrName' 在 setUpControl 的 _throwError (forms.js:1731) (forms.js:1639) 在 FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.addControl (forms.js:4456) 在 FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName._setUpControl (forms.js:4961) 在 FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName.ngOnChanges (forms.js:4911) 在 checkAndUpdateDirectiveInline (core.js:9031) 在 checkAndUpdateNodeInline (core.js:10299) 处 checkAndUpdateNode (core.js:10261) 在 debugCheckAndUpdateNode (core.js:10894) 在 debugCheckDirectivesFn (core.js:10854) 里面 添加PM manager-create-modal.component.ts:50 list after 补充:[对象对象],[对象对象] manager-create-modal.component.ts:53 {MgrName: ""} manager-create-modal.component.ts:53 {MgrName: ""}
我什至不明白为什么没有将元素添加到 manager_formArray。请帮帮我。
【问题讨论】:
-
你忘记了 [formGroupName]="i" in [formGroupName]="i " >
-
@Eliseo 感谢Eliseo,但我仍然遇到同样的错误..
-
按添加按钮时是否收到错误消息?你能控制台记录
addPM-method 中的所有内容吗? -
您正在创建许多
FormGroups,在其中的每个控制器上使用相同的键MgrName。我不确定这是否是一个问题,但您可以尝试在每次创建FormGroup时使用不同的密钥,也许将id添加到名称中?我不确定这是否重要。只是一个想法 -
@John 按下添加按钮时我没有收到错误消息。它导航到 addPM() 方法,但没有任何内容添加到 manager_formArray。获取以下console.log消息:-添加后在addPM manager-create-modal.component.ts:50列表中:[object Object],[object Object],[object Object] manager-create-modal.component.ts:53 {MgrName: ""} manager-create-modal.component.ts:53 {MgrName: ""} manager-create-modal.component.ts:53 {MgrName: ""}
标签: html angular typescript angular6 formarray