Erdem Aydemir
Erdem Aydemir

Reputation: 389

AngularJS - ng-repeat dynamic array value

How can I do if I am trying to combine 2 string values? For example ng-repeat = "a in (name + 'List')". The name string value in this expression allList, jonnyList, henryList. How can I do that?

Example code:

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

Upvotes: 1

Views: 1842

Answers (2)

Slava Utesinov
Slava Utesinov

Reputation: 13488

You can create function, which will return desired array as $scope's corresponding property:

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

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

Upvotes: 4

devnull69
devnull69

Reputation: 16544

Define a scope method that adds the "List" and then use this method in ng-repeat

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

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

Upvotes: 2

Related Questions