【问题标题】:ERROR Error: Cannot find control with path: 'responses -> response' ( Angular 8) FormArray错误错误:找不到带有路径的控件:'responses -> response'(Angular 8)FormArray
【发布时间】:2020-08-18 22:24:12
【问题描述】:

我正在尝试使用 FormArray 来获取使用 for 循环迭代的输入值。我用过primeng table,代码如下

AppComponent HTML

<div formArrayName="responses">
  <p-table [value]="datas">
    <ng-template pTemplate="header">
    <tr style="border: none;">
            <th>Question No</th>
            <th>Questions</th>
            <th>Data</th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-data #input>
      <tr>
        <td>{{data.id}} </td>
        <td [ngClass]="{'objectiveClass' : datas.Type != 'Narrative','narrativeClass':datas.Type == 'Narrative'}">{{ data.Question }}</td>
        <td *ngIf="datas.Type != 'Narrative'">
            <input class="form-control"
             id="objective"
             type="number"
             (keyup)="patternChecking(number)"
             formControlName="response"
             #number/>
        </td>
  <td *ngIf="datas.Type == 'Narrative'">
            <textarea #narrative rows="4" cols="50" maxlength="5000" 
            formControlName="response"
            (keyup) ="charactersRemaining()"
            ></textarea>
            <span  *ngIf = "isCharactersRemaining">
                <em>{{ textMaxLength - narrative.value.length }} characters remaining</em></span>
        </td>
    </tr>
    </ng-template>
</p-table>
</div>

AppComponent TS

 this.responseForm = this.fb.group({
    responses : this.fb.array([this.fb.group({
      response:new FormControl ('')
    })])
   });

我无法识别上述代码的问题。

【问题讨论】:

  • 为什么是两个this.fb

标签: javascript angular forms primeng formarray


【解决方案1】:

FormArray 的 API 真的很笨重。实际上,您必须遍历 FormArray 中的每个控件。比如:

<div *ngFor="let control of responseForm.get('responses').controls; let i = index">
     <div [formGroupName]="i">
          <input
             formControlName="response"
          />
     </div>
</div>

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2018-12-24
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多