Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am new to Angular.js and am trying to create dynamic scope variables in AngularJs inside a for Loop. This is something as below:

$scope.lists=[{listName:'list1'},{listName:'list2'}];

for(var i=0;i<$scope.lists.length;i++){
  var listName = $scope.lists[i].listName;
  listName = $parse(listName);
  listName.assign($scope,[]);
  $scope.$apply();
}

The above code throws an error saying: $digest already in progress.

The code works ok when used without looping just for one as done in: Setting dynamic scope variables in AngularJs - scope.<some_string>

I ultimately am looking for $scope.list1=[] and $scope.list2=[] as 2 separate arrays.

Any leads would be awesome. Thanks.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

The above code throws an error saying: $digest already in progress.

You're already in the controller and in angular scope. So no need to trigger the digest loop using $scope.$apply(). Even if you have to must check the $$phase and then apply.

if (!$scope.$$phase) $scope.$apply()

But for your scenario, it's not required at all

$scope.lists = [{listName: 'list1'}, {listName: 'list2'}];

angular.forEach($scope.lists, function(item) {
    var listName = item.listName;
    $scope[listName] = [];
});
share|improve this answer
    
This does the trick. Thanks. :) –  Vaibhav Apr 18 at 7:26

You don't need $parse and assign here, just use bracket notation to access object property (because $scope is nothing but just an object) with variable name:

$scope.lists = [{listName: 'list1'}, {listName: 'list2'}];

for (var i = 0; i < $scope.lists.length; i++) {
    var listName = $scope.lists[i].listName;
    $scope[listName] = [];
}
share|improve this answer
1  
I don't think that's what he's looking for because he seems to be going for the methodology defined in this post stackoverflow.com/questions/18875486/… –  AR7 Apr 18 at 7:11
    
Actually i am using it with angular dragdrop and by doing what u r saying throws an error saying TypeError: Cannot set property 'jqyoui_pos' of undefined ultimately meaning the $scope.list is undefined. –  Vaibhav Apr 18 at 7:11
    
@AR7 This looks like exactly what he is looking for. The result here will be $scope.list1=[] and $scope.list2=[] as 2 separate arrays. The linked approach would be for implementations where the dot notation is taken into consideration. –  KreepN Apr 18 at 7:18
1  
@Vaibhav Not sure how you are using it after, but above code will create $scope.list1 = [], $scope.list2 = [] properly without any problem. If you have problems that means it's something else outside of this question. –  dfsq Apr 18 at 7:20

The problem is the $scope.$apply(). It starts the angular digest process and then you'll immediately end up calling it again because it's inside a loop. I would recommend moving the statement outside the loop if you don't want to change the way that you're writing your code.

share|improve this answer
    
It's a eye closed typing answer. It is not helped him –  Ramesh Rajendran Apr 18 at 7:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.