Search code examples
javascriptangulartypescriptweb-applications

How to set formControl value dynamically in formGroup using angular?


BuildCustomFields(formControl, formControlValue): FormGroup {
    return this.fb.group({      
      formControl: new FormControl(formControlValue),
    })
  }

I need to add formControl Value from parameter which I pass in BuildCustomFields() method. How can pass the value of formControl dynamically?

It always takes this text "formControl" as key instead of using value which I am passing to it.


Solution

  • If you want formControl to be your dynamic key, you need to pass reference it within [] otherwise you get the value of it

    BuildCustomFields(formControl, formControlValue): FormGroup {
    debugger
    return this.fb.group({
      
     [formControl]: new FormControl(formControlValue),
    })