如何在没有formGroup的情况下控制formArray



在这种情况下,我必须将数据传递到服务器,如下面的[1, 2, 454, 5456]

以及FormGroup 的formArray中的数据

我试图创建一个空数组,然后将空的formControl推送到该数组,但不起作用

this._fb.group({
content: this._fb.group({
question: [null, Validators.required],
media: [null, Validators.required],
options: this._fb.array([new FormControl(null), new FormControl(null), new FormControl(null), new FormControl(null)]),
correct_answer: [null, Validators.required],
})
});

我想要像['1st option', '2nd option']这样的选项数据,但我无法在模板中定义formControlName。如果我定义了一些,它会试图找出formArray的索引的属性。

我的预期结果['1st option', '2nd option']

您只需要在formArray中命名formControls

<! -- inside HTML do --> 
<form [formGroup]="nameofForm" >
<div formGroupName="content"> 
<ng-container formArrayName="options">
<div *ngFor="let controls of nameOfForm.get('options').controls, let i =index"> 
<input type="text">  formControlName="i" />
</div> 
</ng-container>
</div>
</form>

最新更新