2

How can i make angular apply the changes to the view before exiting the click method? In the following example i want the span to be visible while the confirm box is shown.

$scope.click = function () {
    $scope.saving = true; //Not enough

    $scope.$apply(function () {
        $scope.saving = true;
    });//Throws error but works [$rootScope:inprog] $apply already in progress 
    confirm('Are you sure?');

    $scope.saving = false;
}


<span ng-show="saving">Loading ... </span>

Plunker

1 Answer 1

7

What you want to do is doing part of your code in one digest cycle of Angular and the rest in another. I accomplish this by using a small timeout:

$scope.click = function () {
    $scope.saving = true;

    $timeout(function() {
        confirm('Are you sure?');
        $scope.saving = false;
    }, 50);
}

NB: Don't forget to add $timeout to the dependencies of your controller

Sign up to request clarification or add additional context in comments.

Comments

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.