javascript - 必须在索引处为表单控件提供一个值
问题描述
我使用响应式表单,并将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>
解决方案
推荐阅读
- amazon-web-services - 如何使用 lambda 函数和 Cloudformation 访问 S3 端点
- google-oauth - 取消谷歌 OAuth2 授权
- bash - 如何使用远程自动更新本地存储库?
- c# - 在解决方案资源管理器中更改项目文件夹后命名空间未更新
- android - Android RecyclerView 加载时崩溃
- python - 如何使用 python pandas 获取列中缺失的日期
- sql - 从字符串 T-SQL 中删除日期名称
- vitess - Vitess 表等价配置
- c# - 从 C# 列表显示简单的 Google 图表
- arrays - 从 Ruby 中的哈希数组中收集唯一元素