【发布时间】:2021-10-08 18:24:36
【问题描述】:
我正在使用的方法 -- 表单组 -- -- FormControl -- , -- FormArray -- ----------------- -- FormControlList --
我也尝试过嵌套表单组中的 FormArray
但我无法迭代表单数组,如果方法正确,请告诉我。
HTML 错误--:对象可能为“空”- 控件
代码 -
HTML --
<form [formGroup]="abcForm">
<section>
<div formArrayName="title">
<ul>
<li *ngFor="let control of abcForm.get('title').controls; let i= index;">
{{i}}
</li>
</ul>
</div>
</form>
///////////////////
TS --
titles: any = [
{
name: "one",
value: "one",
selected: false
},
{
name: "two",
value: "two",
selected: true
},
{
name: "three",
value: "three",
selected: true
},
];
constructor(private fb: FormBuilder)
this.abcForm = this.fb.group({
one: ['',Validators.required],
title:
this.buildTitleControls()
,
});
buildTitleControls() {
const arr = this.titles.map((val : any) => {
return this.fb.control('');
});
return this.fb.array(arr);
}
【问题讨论】: