Angular 5 动态变量名 [英] Angular 5 Dynamic variable name
本文介绍了Angular 5 动态变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想动态调用一个变量.
导出类 AppComponent {my_a_variable = '测试一个';my_b_variable = '测试 b';类型 = 'a';//这个变量值会动态地来自另一个地方.}
在我的 html 中,我需要类似的东西
{{my_{{type}}_variable}}
我知道它可以用 assoc 数组解决,但我不能在这里使用它.你能帮忙吗?
谢谢.
解决方案
希望这有效,
导出类 AppComponent {my_a_variable = '测试一个';my_b_variable = '测试 b';类型 = 'a';}
在您的模板中,您可以这样做,
{{this['my_' + type + '_variable']}}</div>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.
解决方案 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>
这篇关于Angular 5 动态变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文