gpt4 book ai didi

angular5 - Angular 5 动态变量名

转载 作者:行者123 更新时间:2023-12-05 00:12:59 25 4
gpt4 key购买 nike

我想动态调用一个变量。

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>

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

谢谢。

最佳答案

希望这有效,

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

在您的模板中,您可以这样做,
<div>{{this['my_' + type + '_variable']}}</div>

关于angular5 - Angular 5 动态变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49610238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com