Aviad
Aviad

Reputation: 21

How to access formControl name dynamically?

Is there a way to get the value of a formControl by passing the control name as a function argument?

destructCityAndStreetFromObject(type: string) {
    let form: FormGroup = (this.section2Form.controls.formControlBuilderinputs as FormGroup)
    let Val = form.controls.`${type}`.value.value;
}

Upvotes: 0

Views: 991

Answers (3)

Mohit Yadav
Mohit Yadav

Reputation: 465

function destructCityAndStreetFromObject(type: string) {
 const form = (this.section2Form.controls.formControlBuilderinputs as FormGroup)
 const ctrlType = form.controls[type];
 if(ctrlType){
  const {value:{value: Val}} = ctrlType;
 }
}

Upvotes: 3

Yogendra Chauhan
Yogendra Chauhan

Reputation: 825

form.get("control_name")

Upvotes: 1

Imbro
Imbro

Reputation: 599

let ctrlName = "myControl";    
form.controls[ctrlName].value = ctrlName;

Upvotes: 0

Related Questions