Я использую реактивные формы, и на ngInit
я устанавливаю данные в fieldsngOnInit () {
this.questionControls.controls["inputTitle"].setValue("text"),
this.questionControls.controls["inputPlaceHolder"].setValue("text"),
this.questionControls.controls["selectBoxOptions"].setValue(
this.formBuilder.array(this.getOptionsAsFormGroups())
), --> here i get error
this.questionControls.controls["selectBoxTitle"].setValue("text"),
this.questionControls.controls["checkBoxTitle"].setValue("text"),
this.questionControls.controls["radioBtnTitle"].setValue("text");
}
Но я получаю ошибку
Ошибка: необходимо указать значение для управления формой по индексу: 0.
Вот метод для опций
this.selectedQuestion.options = ['a', 'b', 'c']
private getOptionsAsFormGroups(): FormGroup[] {
let inputs: FormGroup[] = [];
if (this.selectedQuestion.options) {
for (let v of this.selectedQuestion.options)
inputs.push(
this.formBuilder.group({
value: [v]
})
);
} else {
inputs.push(this.getEmptyForm());
}
return inputs;
}
, а вот html
<tr *ngFor="let options of selectOptionsFormGroup.controls; let i = index" [formGroupName]="i">
<td>
<mat-form-field>
<input formControlName="value" type="text" matInput placeholder="option" />
</mat-form-field>
</td>
</tr>