找不到路径为以下位置的控件:angular2 [英] Cannot find control with path: angular2

查看:63
本文介绍了找不到路径为以下位置的控件:angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理模型驱动的表单,但无法将其添加到使用ngFor显示的列表中.尝试对列表进行迭代时,目前出现错误.我尝试了所有可用的解决方案.

I'm working on a model driven form and I can't get it to add items to a list being displayed with ngFor. I'm currently getting an error when trying to iterate the list.I tried all solutions available.

错误

无法找到路径为类别-> 0"的控件

Cannot find control with path: 'categories -> 0'

ts文件

 private categories : any= [{name: 'Strict',selected : false}, 
                                    {name: 'Moderate',selected : true}, 
                                     {name: 'Flexible',selected : true}];

let allCategories: FormArray = new FormArray([]);
          for (let i = 0; i < this.categories.length; i++) {
            let fg = new FormGroup({});
            fg.addControl(this.categories[i].name, new FormControl(this.categories[i].selected))
            allCategories.push(fg)
          }

表单初始化

类别:allCategories,

categories: allCategories,

HTML部分

<div formArrayName="categories">
          <div *ngFor="let category of categories; let i=index">  
          <span formGroupName="{{i}}">             
            <label>
            <input type="checkbox" formControlName="{{category.name}}"/> 
            {{category.name}} 
            </label>                
          </span>
          </div>
      </div>

推荐答案

@Jadoon,我想,@ DineshArun表示他想使用反应性方法列出所有类别,并将该FormArray用于每个类别的FormGroup. 问题是Angular通常将数组索引默认分配为组名0、1 ... 但是在@DineshArun的情况下不起作用.在我的情况下也没有,但是我找到了解决方案.

@Jadoon, I suppose, @DineshArun meant that he wants to list all categories using reactive approach and uses for that FormArray with FormGroups for each category. The problem is that Angular usually assigns array index as group name by default 0, 1... But in @DineshArun's case that doesn't work. And in mine case it didn't too, but I've found the solution.

首先,请查看问题,它被标记为答案.以此方式重写formArray的填充,然后将在patch()方法中指向的控件的原始名称分配给formControlName.

First of all, look at this question and it's marked answer. Rewrite the filling in of your formArray that way, and then assign to formControlName the raw name of the control, which you pointed in your patch() method.

这篇关于找不到路径为以下位置的控件:angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆