Angular 5动态变量名称

Sex*_*yMF 4 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.
}
Run Code Online (Sandbox Code Playgroud)

在我的HTML中,我需要像

<div>{{my_{{type}}_variable}}</div>
Run Code Online (Sandbox Code Playgroud)

我知道可以用assoc数组解决,但是我不能在这里使用它。你能帮忙吗?

谢谢。

Mad*_*n.V 6

希望这行得通,

export class AppComponent  {
 my_a_variable = 'test a';
 my_b_variable = 'test b';
 type = 'a';
}
Run Code Online (Sandbox Code Playgroud)

您可以在模板中这样做,

<div>{{this['my_' + type + '_variable']}}</div>
Run Code Online (Sandbox Code Playgroud)