角度2形式“找不到路径控制". [英] Angular 2 Form "Cannot find control with path"
问题描述
我尝试制作动态表单(因此您可以无限制地将项目添加到列表中),但由于无法找到带有路径的控件,因此我的列表内容未得到发送:
I try to make a dynamic form (so you can limitless add items to a list), but somehow the content of my list is not getting send because it can't find the control with path:
找不到具有以下路径的控件:"list_items-> list_item"
Cannot find control with path: 'list_items -> list_item'
我的组件:
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit {
listForm: FormGroup;
constructor(private nodeService: NodeService, private messageService: MessageService, private fb: FormBuilder,
private listService: ListService) {
this.listForm = this.fb.group({
title: ['', [Validators.required, Validators.minLength(5)]],
list_items: this.fb.array([
this.initListItem(),
])
});
}
initListItem() {
return this.fb.group({
list_item: ['']
});
}
initListItemType() {
return this.fb.group({
list_item_type: ['']
});
}
addListItem() {
const control = <FormArray>this.listForm.controls['list_items'];
control.push(this.initListItem());
}
模板(list.component.html):
The Template (list.component.html):
<h2>Add List </h2>
<form [formGroup]="listForm" novalidate (ngSubmit)="addList(listForm)">
<div class="form-group">
<input type="text" class="form-control" formControlName="title" placeholder="Title">
</div>
<div formArrayName="list_items">
<div *ngFor="let list_item of listForm.controls.list_items.controls; let i=index" class="panel panel-default">
{{i + 1}}.) <input type="text" formControlName="list_item" placeholder="List Item" class="form-control">
</div>
<a (click)="addListItem()">Add List Item +</a>
</div>
<button type="submit">Submit</button>
</form>
标题很好用,但是我找不到"formControlName"出现的错误,这是导致错误的原因.
The title works just fine, but I can't find the error I have with the "formControlName", which is causing the error.
预先感谢您对此问题的帮助.
Thanks in advance for any help in this issue.
更新我所做的更改 list.component.html
Update with what I changed list.component.html
<h2>Add List </h2>
<form [formGroup]="listForm" novalidate (ngSubmit)="addList(listForm)">
<div class="form-group">
<input type="text" class="form-control" formControlName="title" placeholder="Title">
</div>
<div formArrayName="list_items">
<div *ngFor="let list_item of listForm.controls.list_items.controls; let i=index" class="panel panel-default">
{{i + 1}}.) <input type="text" formControlName="{{i}}" placeholder="List Item" class="form-control">
</div>
<a (click)="addListItem()">Add List Item +</a>
</div>
<button type="submit">Submit</button>
</form>
在我的组件中,我更改了构造函数和我的addListItem方法:
And in my component I changed the constructor and my addListItem method:
listForm: FormGroup;
constructor(private nodeService: NodeService, private messageService: MessageService, private fb: FormBuilder,
private listService: ListService) {
this.listForm = this.fb.group({
title: ['', [Validators.required, Validators.minLength(5)]],
list_items: this.fb.array([
[''],
])
});
}
addListItem() {
const control = <FormArray>this.listForm.controls['list_items'];
control.push(this.fb.control(['']));
console.log(control)
}
推荐答案
应该以html格式&您的组件文件.
There should be mapped formControlName in html form & your component file.
<div *ngFor="let list_item of [0,1,2]; let i=index" class="panel panel-default">
{{i + 1}}.) <input type="text" formControlName='{{i}}' placeholder="List Item" class="form-control">
</div>
============
============
list_items: this.fb.array([
[''], //0 points to this
[''], //1 points to this
[''] //2 points to this
])
这篇关于角度2形式“找不到路径控制".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!