0

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 ?

Eyal
  • 3
  • 3
  • 1
    what about `this.myForm.controls.[inputName].setValue(value);` – Sarabadu Feb 19 '20 at 13:56
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors – Wali Waqar Feb 19 '20 at 13:57
  • use a property accessor – Wali Waqar Feb 19 '20 at 13:57
  • try this.myForm.get(inputName).setValue(value) the function get(controlName) return the control with the required name – Mariano Calcagno Feb 19 '20 at 14:08
  • I tried all your method, I know how to use a property accessor but the problem is that it seems that type script doesnt interpret that way.. – Eyal Feb 19 '20 at 15:03
  • TypeScript has nothing to do with it. You're just placing the brackets wrong. `this.myForm.controls[inputRandom.name].setValue(...)`! – deceze Feb 19 '20 at 15:32
  • Many thanks @deceze it works, last thing, should I always acces the propertie of an object, or can I do the same with a simple variable like foo = "myVariableValue" – Eyal Feb 19 '20 at 16:18
  • Are you talking about [*variable variables*](https://stackoverflow.com/q/5187530/476)? Those are virtually always a bad idea. – deceze Feb 19 '20 at 16:19

0 Answers0