2015-08-13 99 views
0

這可能是一個愚蠢的問題,但我無法弄清楚這一點。 我有對象的字符串值的數組:Angular.js,將字符串添加到變量的名稱?

$scope.groups = [{name: "first"}, {name: "second"}]; 

我有這樣一堆項目:

$scope.items = {first: [/*stuff*/], second: [/*stuff*/}; 

「第一」和「第二」是未明確定義,它們被檢索從服務器。 有什麼辦法可以使用$scope.groups[0].name作爲$scope.items.<here>的一部分嗎?或者也許有更好的解決方案?

回答

2

您可以使用JavaScript關聯數組語法:

$scope.items[$scope.groups[0].name] 

您可以利用此格式都得到和$scope.items設置的值。當然,在嘗試此操作之前,您至少需要通過$scope.items = {};進行初始化。

0

是的,你可以如下定義它:

$scope.items[$scope.groups[0].name] = [/*stuff*/];