全局定义一个FormGroup,然后根据不同的数据,动态在FormGroup中添加FormControl。 public formGroup: FormGroup; this.xxxService.getData().subscribe((data: Array<string>) => { this.formGroup = new FormGroup({}); data.forEach((key: string) => { this.formGroup.addControl(key, new FormControl('')); }); });
全局定义一个
FormGroup
,然后根据不同的数据,动态在
FormGroup
中添加FormControl
。