Angular 5动态变量名称

时间:2018-04-02 11:16:28

标签: angular5

我想动态调用变量。

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

在我的HTML中,我需要像

这样的东西
<div>{{my_{{type}}_variable}}</div>

我知道它可以通过一个关联数组来解决,但我不能在这里使用它。 你能帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:3)

希望这有效,

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

在你的模板中你可以这样做,

<div>{{this['my_' + type + '_variable']}}</div>