我正在使用 Angular5,并且我有一个字段列表,每个字段都有一个名称和 FormControl。我尝试使用此代码将控件动态添加到组中,但出现错误。
const formControlFields = [
{name: 'field1',control: [null, []] as FormControl},
{name: 'field2',control: [null, []] as FormControl}
];
const formGroup: FormGroup = new FormGroup({});
formControlFields.forEach( f => formGroup.addControl(f.name,f.control));
this.form = new FormGroup({groups:formGroup});
这是我得到的错误:
错误类型错误:control.setParent 不是函数
at FormGroup.registerControl (forms.js:4352) at FormGroup.addControl (forms.js:4372) at eval (trade-search.component.ts:142)