4

I want to call a variable dynamically.

export class AppComponent  {
  my_a_variable = 'test a';
  my_b_variable = 'test b';
  type = 'a'; // this variable value will come from another place dynamically.
}

In my html i need something like

<div>{{my_{{type}}_variable}}</div>

I know it can be solved with an assoc array but I can't use this here. Can you please help?

Thanks.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • 1
    This is already asked [https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript](https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript) – Vipul Solanki Apr 02 '18 at 11:37
  • @VipulSolanki this is about angular variable binding. nothing to do with it. – SexyMF Apr 02 '18 at 11:46

1 Answers1

12

Hope this works,

export class AppComponent  {
 my_a_variable = 'test a';
 my_b_variable = 'test b';
 type = 'a';
}

In your template you can do like this,

<div>{{this['my_' + type + '_variable']}}</div>
Madhavan.V
  • 825
  • 1
  • 6
  • 14