Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to patch form values with dynamic form control names

I have this form:

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

I have this method:

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

I have this map:

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

I'm attempting to update the controls in the form via a variable, but angular seems to not allow me to dynamically name form controls. It's taking control as an actual control named: "control". Can I do what I want in angular?

like image 637
tblev Avatar asked Sep 08 '25 11:09

tblev


1 Answers

You need brackets around control. See computed property names over at MDN

setControlValue(control: string, value: string){
  this.profileSettingsGroup.patchValue({
    [control]: value
  });
}
like image 54
ihorbond Avatar answered Sep 11 '25 04:09

ihorbond