繁体   English   中英

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

[英]How to patch form values with dynamic form control names

我有这个表格:

public profileSettingsGroup = new FormGroup({
    firstName: new FormControl('Jonathon', Validators.required)
})

我有这个方法:

setControlValue(control: string, value: string){
  this.profileSettingsGroup.patchValue({
    control: value
  });
}

我有这个 map:

for (let control in this.profileSettingsGroup.controls) {
  this.map.set(control, this.camelCase(control));
}

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

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

setControlValue(control: string, value: string){
  this.profileSettingsGroup.patchValue({
    [control]: value
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM