0

I have a list :

$scope.selectlist = ["list1","list2","list3"];

Once I choose an element, I should display the list according to the select value knowing that I have these lists in my scope :

$scope.list1
$scope.list2
$scope.list3 

Any ideas to make it work ?

I thought doing :

    <div ng-repeat="i in selectedPerson">
       {{selectedPerson}}
    </div>

will work.

Fiddle

4 Answers 4

2

You can create a variable that holds all lists $scope.allLists and after a list is selected $scope.selectedList, just use the following syntax $scope.allLists[$scope.selectedList].

For example $scope.allLists = { list1: [...], list2: [...], list3: [...] } and $scope.selectedList = 'list1'.

Sign up to request clarification or add additional context in comments.

Comments

1

Your ng-repeat variable is i so You should put {{i}} instead of {{selectedPerson}}.

You should also get proper list from the scope, based on $scope.selectedPerson value. Try this :

$scope.getList = function() { return $scope[$scope.selectedPerson]; }

Fiddle

Comments

1

Change your template to this:

<div ng-app="myapp" ng-controller="FirstCtrl">
        <select ng-options="p for p in selectlist" ng-model="selectedPerson"></select>

        <div ng-repeat="i in this[selectedPerson]">
           {{i}}
        </div>
</div>

Comments

1

You just need to use javascript variable instead of $scope for your list1, list2, list3.

var list1 = [1,2,3,4,5];

var list2 = ['a','b','c','d','e','f','g'];

var list3 = ["hi","bye","whatever"];

$scope.selectlist = [list1,list2,list3];

http://jsfiddle.net/81t6cbjw/70/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.