如何使用动态表单控件名称修补表单值

tbl*_*lev 1 javascript angular

我有这个表格:

public profileSettingsGroup = new FormGroup({
    firstName: new FormControl('Jonathon', Validators.required)
})
Run Code Online (Sandbox Code Playgroud)

我有这个方法:

setControlValue(control: string, value: string){
  this.profileSettingsGroup.patchValue({
    control: value
  });
}
Run Code Online (Sandbox Code Playgroud)

我有这张地图:

for (let control in this.profileSettingsGroup.controls) {
  this.map.set(control, this.camelCase(control));
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试通过变量更新表单中的控件,但 angular 似乎不允许我动态命名表单控件。它以control实际控件命名:“控件”。我可以在 angular 中做我想做的事吗?

iho*_*ond 5

你需要控制周围的括号。在 MDN 上查看计算属性名称

setControlValue(control: string, value: string){
  this.profileSettingsGroup.patchValue({
    [control]: value
  });
}
Run Code Online (Sandbox Code Playgroud)