Javascript 必须在索引处为窗体控件提供值
我使用反应式表单,并在Javascript 必须在索引处为窗体控件提供值,javascript,angular,angular-reactive-forms,Javascript,Angular,Angular Reactive Forms,我使用反应式表单,并在ngInit中将数据设置为fieldsngOnInit(){ 但我有一个错误 错误:必须在索引0处为窗体控件提供值 下面是选项的方法 this.selectedQuestion.options = ['a', 'b', 'c'] private getOptionsAsFormGroups(): FormGroup[] { let inputs: FormGroup[] = []; if (this.selectedQuestion.opti
ngInit
中将数据设置为fieldsngOnInit(){
但我有一个错误
错误:必须在索引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>
正如错误所说,如果您向输入元素“提供一个值”属性会发生什么情况?如果您的意思是这样的
..??我得到sam错误,感觉有一些子表单可能会帮助您。如果是这样的话,请检查
<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>