AngularJS:使用名称的变量他人的变量值 [英] AngularJS: Name a variable using another's variable value
问题描述
我是新来AngularJS我还在探索其可能性。目前我坚持了这个问题:我如何可以使用其他的变量值名称的变量
I'm new to AngularJS and I'm still exploring its possibilities. Currently I'm stuck with this problem: How can I name a variable using another's variable value?
例如我有我的控制器是这样的:
For example I have something like this in my controller:
$scope.item_foo = 'Foo';
$scope.item_bar = 'Bar';
我可以用这个访问值:
I can access the values with this:
{{item_foo}}
{{item_bar}}
到目前为止好,但现在我要动态地显示它们。我与'富'和'酒吧',并与ngRepeat我环路它的对象。我怎样才能做这样的事情:
So far so good, but now I want to display them dynamically. I have an object with 'foo' and 'bar' and I loop it with ngRepeat. How can I do something like that:
{{item_{{variable_with_value_foo_or_bar}}}} // should print Foo and Bar
感谢您对您的帮助!
推荐答案
在您的模板,这个
指的是当前的范围,所以你可以这样做:
Within your template, this
refers to the current scope, so you can do:
{{ this[ 'item_' + variable_with_value_foo_or_bar ] }}
有可能是更好的方法来完成你想要做什么,但希望这回答你的问题。
There are probably better ways to perform what you're trying to do, but hopefully this answers your question.
这篇关于AngularJS:使用名称的变量他人的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!