2017-04-24 44 views
1

如果我試圖組合2個字符串值,該怎麼辦?例如ng-repeat = "a in (name + 'List')"。該表達式中的名稱字符串值allListjonnyListhenryList。我怎樣才能做到這一點?AngularJS-ng-repeat動態數組值

示例代碼:

<ul ng-repeat="a in name+List"> // i want if "name = 'all'", Return the values in allList. 
    <li>{{a}}</li> 
</ul> 
+0

發佈小提琴或在這裏添加一些虛擬數據 –

回答

3

可以創建功能,這將返回所需的陣列作爲$scope's相應的屬性:

$scope.dynamicArray = function(name){ 
    return $scope[name + 'List']; 
} 

<ul ng-repeat="a in dynamicArray(name)"> 
    <li>{{a}}</li> 
</ul> 
+0

我是個白癡。非常感謝。 – forguta

2

定義,增加了「列表」一個範圍的方法,然後使用此方法in ng-repeat

$scope.getList = function(name) { 
    return $scope[name + "List"]; 
}; 

<ul ng-repeat="a in getList(name)"> 
    <li>{{a}}</li> 
</ul> 
+0

非常感謝。 – forguta