Rendy Rendy - 4 months ago 18
AngularJS Question

Map depends on dynamic variable

I have following hardcoded map:

$scope.lists = {
test1: [
{ name: 'My Name' }
],
test2: [
{ name: 'My Second Name' }
]
};

$scope.selectedList = "test1";


Then inside my pug/html file:

div(ng-repeat="list in lists.selectedList")


What I need is I want to have array to be selected depend on var of
selectedList
. Am I able to do that in AngularJS?

Answer

You can use square-bracket syntax in angular attributes:

 div(ng-repeat="list in lists[selectedList]")