首页 > 解决方案 > 必须在索引处为表单控件提供一个值

问题描述

我使用响应式表单,并将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");
}

但我得到错误

ERROR 错误:必须为索引处的表单控件提供值: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>

标签: javascriptangularangular-reactive-forms

解决方案


推荐阅读