Ошибка: не удается найти элемент управления с путем: «отклики -> ответ» (Angular 8) FormArray - PullRequest
1 голос
/ 04 мая 2020

Я пытаюсь использовать FormArray, чтобы получить значение входных данных, которые были повторены для l oop. Я использовал основную таблицу. Код выглядит следующим образом:

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 ('')
    })])
   });

Я не могу идентифицировать проблему с кодом выше.

enter image description here

1 Ответ

0 голосов
/ 04 мая 2020

API для FormArray действительно неуклюжий. Вы на самом деле должны перебирать каждый элемент управления внутри FormArray. Что-то вроде:

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