在模板中传递动态formControl值

时间:2019-10-07 12:40:55

标签: angular typescript angular-reactive-forms ngfor

单击按钮时如何传递确切的文本区域值?

<form novalidate [formGroup]="simulationForm">
     <div  formArrayName="answer_key"  *ngFor="let other of simulationForm.get('answer_key').controls; let i = index" class="form-group">

          <div [formGroupName]="i" class="mat-form-field--inline">
                    {{i + 1}}.
                    <mat-form-field>
                        <textarea matInput placeholder="Sentence" id="sentence" formControlName="sentence" readonly="true"></textarea>
                    </mat-form-field>
          <button class="btn btn-success" type="button" (click)="openDialog()">Add</button>
                </div>  
          </div>
</form>

想要在单击每个按钮时传递每一行的句子值。

类似的东西

(click)="openDialog(simulationForm.get('sentence').value)"

(click)="openDialog(simulationForm.get('answer_key').controls['sentence'].value)"

1 个答案:

答案 0 :(得分:3)

(click)="openDialog(i)"

openDialog(index:number)
{
   const text=(simulationForm.get('answer_key') as FormArray)
       .at(index).get('sentence').value

   //or 
   const text=(simulationForm.get('answer_key') as FormArray)
       .at(index).value.sentence

   //I'm not sure about
   const text=simulationForm.value.answer_key[index].sentence


}