【问题标题】:Angular 2 Reactive Forms cannot find controlAngular 2 Reactive Forms 找不到控件
【发布时间】:2017-06-15 01:31:27
【问题描述】:

我是 Reactive Forms 的新手,我正在尝试制作一个具有 indicadoresanswers 的组件:

有了这个组件:

addAnswers(indicador: Indicador, answer?: any):void {
    const indicadoresFormArray = <FormArray>this.customForm.controls['indicadores'];

    let label = answer ? answer.label : '';
    let value = answer ? answer.value : '';

    let superi = 0;
    for(let i = 0; i < indicadoresFormArray.length; i++) {
        if(indicadoresFormArray.value[i].id == indicador.id) {
            superi = i;
        }
    }

    (<FormArray>(<FormGroup>(indicadoresFormArray).controls[superi])
    .controls['answers']).push(
        new FormGroup({
            label: new FormControl(label),
            value: new FormControl(value)
        })
    )
}

还有模板

<div *ngFor="let indicador of customForm.controls['indicadores'].controls">
<div class="row" *ngFor="let answer of indicador.controls['answers'].controls">
    <div class="input-field col m5 s6"><input formControlName="label" placeholder="Etiqueta" /></div>
    <div class="input-field col m5 s6"><input formControlName="value" placeholder="Valores" /></div>
    <div class="input-field col m2 s12 center-align">
        <button type="button" class="btn-floating waves-effect waves-light" (click)="addAnswer()"><i class="material-icons">add</i></button>
    </div>
</div>
</div>

总是抛出异常:

ERROR Error: Cannot find control with name: 'label'
ERROR Error: Cannot find control with name: 'value'

我也不知道为什么……

console.log(indicadoresFormArray);

【问题讨论】:

  • 公共自定义表单:FormGroup; constructor(private fb: FormBuilder) { this.customForm = this.fb.group({ label : [null], value : ['', Validators.required] }) } 试试这个

标签: angular angular-reactive-forms


【解决方案1】:

您的模板存在一些问题,缺少一些 formArrayNameformGroupName

每个FormArray 都需要在模板formArrayName="the name of the array" 中进行标记,如果您在数组中嵌套了FormGroup,则在这种情况下需要使用索引(您从FormArray),像这样:[formGroupName]="i"formGroupName="{{i}}"

所以你的模板应该如下所示:

<!-- Mark formarray before iteration -->
<div formArrayName="indicadores">
  <!-- Mark formGroupName in a div inside iteration or on the same line -->
  <div *ngFor="let ctrl of customForm.get('indicadores').controls; let i = index" formGroupName="{{i}}">
    <label>Predefined</label>
    <input type="checkbox" formControlName="predefined">
    <!-- Again mark the formarray.... and formgroupname below that -->
    <div formArrayName="answers">
      <div *ngFor="let cont of ctrl.controls.answers.controls; let j=index" formGroupName={{j}}>
        <input formControlName="label" placeholder="Etiqueta" />
        <input formControlName="value" placeholder="Valores" />        
      </div>
    </div>
  </div>
</div>

PLUNKER

【讨论】:

  • 感谢您的回复,我收到错误消息:ERROR Error: Cannot find control with path: '0 -&gt; label'
  • 您能否提供一个有效的 plunker 来展示这个问题,我很乐意看看它。当你有东西要测试时,帮助更容易:)
  • 我正在尝试制作一个 plnkr 但无法正常工作,这实际上是我的工作:plnkr.co/edit/w7wJNRKmwwLkkN3AgUy3?p=preview 按照我之前回答的说明进行操作
  • 好吧,它不像你说的那样工作,你至少是 plunker 中的initForm 函数:)
  • 似乎所有的 plunker 都不适合我,所以我不能 fork 我的 plunker 给你看。但是您应该能够在indicadores:&lt;h5&gt;{{ctrl.value.IndicadorNombre}}&lt;/h5&gt; 和 div:&lt;div *ngIf="ctrl.value.predefined"&gt;It's true!&lt;/div&gt; 的迭代中执行以下操作
猜你喜欢
  • 2017-10-28
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2018-08-24
  • 2023-01-23
  • 1970-01-01
相关资源
最近更新 更多