Hamza BENDALI BRAHAM
Hamza BENDALI BRAHAM

Reputation: 152

i have summ difficult to run $apply

i have list of element when i click in one of them, i fill the template then i copy it to the new DIV, i got an empty template, when i use $scope.$apply() i got an error.

$scope.tache_list.forEach(element => {
  $scope.var1 = element;  
  $scope.$apply();
  $('#div2').append($("#div1").html());  
});

i got Error $rootScope:inprog , what i can do ?

Error rootScope

Upvotes: 0

Views: 43

Answers (3)

Hamza BENDALI BRAHAM
Hamza BENDALI BRAHAM

Reputation: 152

i resovle this probleme by

$timeout(function(){
  $scope.$apply()
})
.then(function(){
  ...
});

thanks for all.

Upvotes: 0

Ashish Santikari
Ashish Santikari

Reputation: 443

Wrap your $scope.$apply call inside a $timeout function.

$timeout(function(){
   $scope.$apply()
});

Reason: Digest cycle will be moved to event loop and execute when the existing cycle completes.

Upvotes: 1

Jordy van Eijk
Jordy van Eijk

Reputation: 2766

Please take a look at the following article about $digest and $apply

Your inprogress error is because you call $apply() from inside an $apply block. You only want to call the $apply from outside angular code that starts a new turn. So if you have a setTimeout() in your forEach you can call $apply inside the setTimeout to tell angular you want it to update.

Upvotes: 1

Related Questions