Angular 2表单“无法找到路径控制”

前端之家收集整理的这篇文章主要介绍了Angular 2表单“无法找到路径控制”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试制作一个动态表单(这样你就可以无限制地将项目添加到列表中),但不知何故我的列表内容没有得到发送,因为它无法找到带路径的控件:

Cannot find control with path: ‘list_items -> list_item’

我的组件:

  1. @Component({
  2. selector: 'app-list',templateUrl: './list.component.html',styleUrls: ['./list.component.css']
  3. })
  4. export class ListComponent implements OnInit {
  5.  
  6. listForm: FormGroup;
  7.  
  8. constructor(private nodeService: NodeService,private messageService: MessageService,private fb: FormBuilder,private listService: ListService) {
  9. this.listForm = this.fb.group({
  10. title: ['',[Validators.required,Validators.minLength(5)]],list_items: this.fb.array([
  11. this.initListItem(),])
  12. });
  13. }
  14.  
  15.  
  16. initListItem() {
  17. return this.fb.group({
  18. list_item: ['']
  19. });
  20. }
  21. initListItemType() {
  22. return this.fb.group({
  23. list_item_type: ['']
  24. });
  25. }
  26.  
  27. addListItem() {
  28. const control = <FormArray>this.listForm.controls['list_items'];
  29. control.push(this.initListItem());
  30. }

模板(list.component.html):

  1. <h2>Add List </h2>
  2.  
  3. <form [formGroup]="listForm" novalidate (ngSubmit)="addList(listForm)">
  4. <div class="form-group">
  5. <input type="text" class="form-control" formControlName="title" placeholder="Title">
  6. </div>
  7.  
  8. <div formArrayName="list_items">
  9. <div *ngFor="let list_item of listForm.controls.list_items.controls; let i=index" class="panel panel-default">
  10. {{i + 1}}.) <input type="text" formControlName="list_item" placeholder="List Item" class="form-control">
  11. </div>
  12. <a (click)="addListItem()">Add List Item +</a>
  13.  
  14. </div>
  15.  
  16. <button type="submit">Submit</button>
  17. </form>

标题工作正常,但我找不到我对“formControlName”的错误,这导致错误.

在此问题上提前感谢您的任何帮助.

更新我改变的内容
list.component.html

  1. <h2>Add List </h2>
  2.  
  3. <form [formGroup]="listForm" novalidate (ngSubmit)="addList(listForm)">
  4. <div class="form-group">
  5. <input type="text" class="form-control" formControlName="title" placeholder="Title">
  6. </div>
  7.  
  8. <div formArrayName="list_items">
  9. <div *ngFor="let list_item of listForm.controls.list_items.controls; let i=index" class="panel panel-default">
  10. {{i + 1}}.) <input type="text" formControlName="{{i}}" placeholder="List Item" class="form-control">
  11. </div>
  12. <a (click)="addListItem()">Add List Item +</a>
  13.  
  14. </div>
  15.  
  16. <button type="submit">Submit</button>
  17. </form>

在我的组件中,我更改了构造函数和我的addListItem方法

  1. listForm: FormGroup;
  2.  
  3. constructor(private nodeService: NodeService,list_items: this.fb.array([
  4. [''],])
  5. });
  6. }
  7.  
  8. addListItem() {
  9. const control = <FormArray>this.listForm.controls['list_items'];
  10. control.push(this.fb.control(['']));
  11. console.log(control)
  12. }
应该以html格式映射formControlName&你的组件文件.
  1. <div *ngFor="let list_item of [0,1,2]; let i=index" class="panel panel-default">
  2.  
  3. {{i + 1}}.) <input type="text" formControlName='{{i}}' placeholder="List Item" class="form-control">
  4. </div>

============

  1. list_items: this.fb.array([
  2. [''],//0 points to this
  3. [''],//1 points to this
  4. [''] //2 points to this
  5. ])

猜你在找的Angularjs相关文章