Quantcast
Channel: AngularJS : Prevent error $digest already in progress when calling $scope.$apply() - Stack Overflow
Viewing all articles
Browse latest Browse all 29

Answer by Eduardo Eljaiek for AngularJS : Prevent error $digest already in progress when calling $scope.$apply()

$
0
0

try using

$scope.applyAsync(function() {
    // your code
});

instead of

if(!$scope.$$phase) {
  //$digest or $apply
}

$applyAsync Schedule the invocation of $apply to occur at a later time. This can be used to queue up multiple expressions which need to be evaluated in the same digest.

NOTE: Within the $digest, $applyAsync() will only flush if the current scope is the $rootScope. This means that if you call $digest on a child scope, it will not implicitly flush the $applyAsync() queue.

Exmaple:

  $scope.$applyAsync(function () {
                if (!authService.authenticated) {
                    return;
                }

                if (vm.file !== null) {
                    loadService.setState(SignWizardStates.SIGN);
                } else {
                    loadService.setState(SignWizardStates.UPLOAD_FILE);
                }
            });

References:

1.Scope.$applyAsync() vs. Scope.$evalAsync() in AngularJS 1.3

  1. AngularJs Docs

Viewing all articles
Browse latest Browse all 29

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>