Edit : My first question was closed and linked to : Dynamically access object property using variable but I dont think this resolve my problem :
Initial post :
kinda new to angular and typescript, I'm strugling with something without finding the correct answer. I want to use the value of a variable in some functions/directives, this is vague so let's illustrate with an exemple:
Lets take a simple setter function for an angular form where a click on a button call a setter for an input.
setInput1(value){ this.myForm.controls.inputName1.setValue(value); };
My question is simple, if I have multiple inputs, instead of creating multiple functions like bellow:
setInput1(value){ this.myForm.controls.inputName1.setValue(value); }; setInput2(value){ this.myForm.controls.inputName2.setValue(value); }; setInput3(value){ this.myForm.controls.inputName3.setValue(value); };
Can I use the value of a variable {inputName} inside my function:directive in some way, like below ?
setInputs(value, inputName){ this.myForm.controls.{inputName}.setValue(value); };
Corrections after reading the link and some more html, using the setValue() method of angular FormControl for the sake of explanations :
html :
<label>random input name 1
<input type="number" formControlName="inputName1" class="form-input">
</label>
<label>random input name 2
<input type="number" formControlName="inputName2" class="form-input">
</label>
<label>random input name 3
<input type="number" formControlName="inputName3" class="form-input">
</label>
TypeScript :
mySetValue(btnValue){
let inputRandom :any = {name : 'inputName1'};
this.myForm.controls.inputRandom[name].setValue(btnValue);
};
Using the bracket, does not work either. It seems typescript doesnt interprete it like I want to because I want this :
this.myForm.controls.inputRandom[name].setValue(btnValue);
to be interpret as :
this.myForm.controls.inputName1.setValue(btnValue);
Please, can someone be more specific if I'm wrong in understanding what was linked to me ?