Angular 5 Dynamic variable name

后端 未结 1 1653
眼角桃花
眼角桃花 2021-01-19 12:26

I want to call a variable dynamically.

export class AppComponent  {
  my_a_variable = \'test a\';
  my_b_variable = \'test b\';
  type = \'a\'; // this var         


        
相关标签:
1条回答
  • 2021-01-19 13:24

    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>
    
    0 讨论(0)
提交回复
热议问题