Angular 5 动态变量名称

4
我想动态调用一个变量。
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
这个问题已经被问过了:https://dev59.com/nW435IYBdhLWcg3w9FBY - Vipul Solanki
@VipulSolanki 这是关于Angular变量绑定的问题,与此无关。 - SexyMF
1个回答

12
希望这能起作用,
export class AppComponent  {
 my_a_variable = 'test a';
 my_b_variable = 'test b';
 type = 'a';
}
在您的模板中,您可以这样做,
<div>{{this['my_' + type + '_variable']}}</div>

什么是+类型+语法与字符串插值相关,鼓励提供任何文档。 - md venkatesh
@mdvenkatesh 这只是字符串连接。 - Madhavan.V

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接