0

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?

0

1 Answer 1

1

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()] = [];
    }    
}
Sign up to request clarification or add additional context in comments.

2 Comments

I like it. I'm still new to Angular, I didn't think of doing that.
It's all good :) this is something you can do in plain old javascript too :)

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.