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.

2

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

what is + type + syntax is something related to sting interpolation,Any documention is encorged
@mdvenkatesh It is just the string concatenation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.