user727728
user727728

Reputation: 743

Correct way to update scope value in angularjs view

View code

 <a ng-click="deleteOrdersUpdate()">Update</a>
 <span class="red">{{noOfOrders}}</span>

Controller code

 $scope.deleteOrdersUpdate=function () {
 $scope.noOfOrders=$scope.selection.length;
 scope.$apply();
 };   

I keep getting $rootScope:inprog Action Already In Progress error.

What is the correct way to do this?Can any one please point me in the right direction.

Upvotes: 0

Views: 74

Answers (3)

CodePrimate
CodePrimate

Reputation: 6666

There's no reason to call $apply here as you're already operating within the angular context.

$scope.deleteOrdersUpdate = function () {
     $scope.noOfOrders = $scope.selection.length;
 };   

Upvotes: 3

Ashisha Nautiyal
Ashisha Nautiyal

Reputation: 1397

This is beacase $apply already in progress.you have to check the phase first .Here is good explanation and implement

angularjs-apply-digest-in-detail

Upvotes: 0

Vamsi
Vamsi

Reputation: 9780

You have to check if it is already in any digest cycle, if it is then it will be automatically done, you don't need to do this, if not then you can apply the scope

if(!$scope.$$phase) {
    $scope.$apply();
}

Upvotes: 0

Related Questions