AngularJS : Prevent error $digest already in progress when calling $scope.$apply()

前端 未结 28 2711
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 22:31

I\'m finding that I need to update my page to my scope manually more and more since building an application in angular.

The only way I know of to do this is to call

相关标签:
28条回答
  • 2020-11-21 23:26

    Don't use this pattern - This will end up causing more errors than it solves. Even though you think it fixed something, it didn't.

    You can check if a $digest is already in progress by checking $scope.$$phase.

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

    $scope.$$phase will return "$digest" or "$apply" if a $digest or $apply is in progress. I believe the difference between these states is that $digest will process the watches of the current scope and its children, and $apply will process the watchers of all scopes.

    To @dnc253's point, if you find yourself calling $digest or $apply frequently, you may be doing it wrong. I generally find I need to digest when I need to update the scope's state as a result of a DOM event firing outside the reach of Angular. For example, when a twitter bootstrap modal becomes hidden. Sometimes the DOM event fires when a $digest is in progress, sometimes not. That's why I use this check.

    I would love to know a better way if anyone knows one.


    From comments: by @anddoutoi

    angular.js Anti Patterns

    1. Don't do if (!$scope.$$phase) $scope.$apply(), it means your $scope.$apply() isn't high enough in the call stack.
    0 讨论(0)
  • 2020-11-21 23:26

    Many of the answers here contain good advices but can also lead to confusion. Simply using $timeout is not the best nor the right solution. Also, be sure to read that if you are concerned by performances or scalability.

    Things you should know

    • $$phase is private to the framework and there are good reasons for that.

    • $timeout(callback) will wait until the current digest cycle (if any) is done, then execute the callback, then run at the end a full $apply.

    • $timeout(callback, delay, false) will do the same (with an optional delay before executing the callback), but will not fire an $apply (third argument) which saves performances if you didn't modify your Angular model ($scope).

    • $scope.$apply(callback) invokes, among other things, $rootScope.$digest, which means it will redigest the root scope of the application and all of its children, even if you're within an isolated scope.

    • $scope.$digest() will simply sync its model to the view, but will not digest its parents scope, which can save a lot of performances when working on an isolated part of your HTML with an isolated scope (from a directive mostly). $digest does not take a callback: you execute the code, then digest.

    • $scope.$evalAsync(callback) has been introduced with angularjs 1.2, and will probably solve most of your troubles. Please refer to the last paragraph to learn more about it.

    • if you get the $digest already in progress error, then your architecture is wrong: either you don't need to redigest your scope, or you should not be in charge of that (see below).

    How to structure your code

    When you get that error, you're trying to digest your scope while it's already in progress: since you don't know the state of your scope at that point, you're not in charge of dealing with its digestion.

    function editModel() {
      $scope.someVar = someVal;
      /* Do not apply your scope here since we don't know if that
         function is called synchronously from Angular or from an
         asynchronous code */
    }
    
    // Processed by Angular, for instance called by a ng-click directive
    $scope.applyModelSynchronously = function() {
      // No need to digest
      editModel();
    }
    
    // Any kind of asynchronous code, for instance a server request
    callServer(function() {
      /* That code is not watched nor digested by Angular, thus we
         can safely $apply it */
      $scope.$apply(editModel);
    });
    

    And if you know what you're doing and working on an isolated small directive while part of a big Angular application, you could prefer $digest instead over $apply to save performances.

    Update since Angularjs 1.2

    A new, powerful method has been added to any $scope: $evalAsync. Basically, it will execute its callback within the current digest cycle if one is occurring, otherwise a new digest cycle will start executing the callback.

    That is still not as good as a $scope.$digest if you really know that you only need to synchronize an isolated part of your HTML (since a new $apply will be triggered if none is in progress), but this is the best solution when you are executing a function which you cannot know it if will be executed synchronously or not, for instance after fetching a resource potentially cached: sometimes this will require an async call to a server, otherwise the resource will be locally fetched synchronously.

    In these cases and all the others where you had a !$scope.$$phase, be sure to use $scope.$evalAsync( callback )

    0 讨论(0)
  • 2020-11-21 23:26

    This is my utils service:

    angular.module('myApp', []).service('Utils', function Utils($timeout) {
        var Super = this;
    
        this.doWhenReady = function(scope, callback, args) {
            if(!scope.$$phase) {
                if (args instanceof Array)
                    callback.apply(scope, Array.prototype.slice.call(args))
                else
                    callback();
            }
            else {
                $timeout(function() {
                    Super.doWhenReady(scope, callback, args);
                }, 250);
            }
        };
    });
    

    and this is an example for it's usage:

    angular.module('myApp').controller('MyCtrl', function ($scope, Utils) {
        $scope.foo = function() {
            // some code here . . .
        };
    
        Utils.doWhenReady($scope, $scope.foo);
    
        $scope.fooWithParams = function(p1, p2) {
            // some code here . . .
        };
    
        Utils.doWhenReady($scope, $scope.fooWithParams, ['value1', 'value2']);
    };
    
    0 讨论(0)
  • 2020-11-21 23:27

    When I disabled debugger , the error is not happening anymore. In my case, it was because of debugger stopping the code execution.

    0 讨论(0)
提交回复
热议问题