2013-10-22 41 views
0

比方說,例如,您有一個包含"father": {"name":"Bob"}的.json文件和一個從中獲取數據並將其存儲在名爲家庭的數組中的JavaScript文件。angular.js中的大括號

{{family.father.name}}會在html頁面上打印父親的名字。

是否可以做這樣的事情:

在JavaScript文件:

$scope.member = "father"; 

index.html中:

{{family.{{member}}.name}} 
// Is there some way to replace father with a variable 
// from the javascript file? Just curious. 
+0

你說',並將其存儲在一個名爲family'的數組。你的意思是「一個叫做家庭的對象」嗎?因爲如果'family'確實是一個數組,那麼字典符號將不起作用。你將不得不搜索數組,直到找到你正在尋找的'member' ... – tennisgent

+0

是的,我的意思是說對象。 – seeplusplus

+0

好的,那麼是的,@ vittore的答案將起作用。但是你需要綁定大括號:'{{family [member] .name}}' – tennisgent

回答

2

使用的字典方式,是

var member = 'father' 

family[member].name === family.father.name 

或在角

JS:

$scope.member = 'father' 
$scope.family = { 'father': { name : 'Bob' } } 

HTML:

{{ family[member].name }} 

將輸出 '鮑勃'