I have a $scope of 1 to 5 lists, depending on user input. In each list there will be an array of items. I have my list names as list1, list2....My problem is when I declare the $scope using $scope.list = []; it of course isn't going to know which list that would be. Since I am naming them statically and they have a limit of 5 I know I could declare each list. I feel that is a bit too heavy and not efficient. Is there a better way of declaring each list, based on the user input?
Add a comment
|
1 Answer
You can declare a property on your $scope dynamically - as with any other javascript object.
so, $scope.myList = [] is the same as $scope['myList'] = []
using your users input it should be simple to create these list properties on your scope from your users input.
Psuedo-code could be:
$scope.myButtonClick = function(){
// where userInput is a number
for (i = 0; i < userInput; i++){
$scope['list' + (i+1).toString()] = [];
}
}
2 Comments
Taylor Mitchell
I like it. I'm still new to Angular, I didn't think of doing that.
Darren Wainwright
It's all good :) this is something you can do in plain old
javascript too :)