Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 Dynamic variable name

Tags:

angular5

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.

like image 369
SexyMF Avatar asked Jan 28 '23 09:01

SexyMF


1 Answers

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>
like image 195
Madhavan.V Avatar answered Mar 17 '23 02:03

Madhavan.V