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

前端 未结 28 2694
伪装坚强ぢ
伪装坚强ぢ 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:04

    yearofmoo did a great job at creating a reusable $safeApply function for us :

    https://github.com/yearofmoo/AngularJS-Scope.SafeApply

    Usage :

    //use by itself
    $scope.$safeApply();
    
    //tell it which scope to update
    $scope.$safeApply($scope);
    $scope.$safeApply($anotherScope);
    
    //pass in an update function that gets called when the digest is going on...
    $scope.$safeApply(function() {
    
    });
    
    //pass in both a scope and a function
    $scope.$safeApply($anotherScope,function() {
    
    });
    
    //call it on the rootScope
    $rootScope.$safeApply();
    $rootScope.$safeApply($rootScope);
    $rootScope.$safeApply($scope);
    $rootScope.$safeApply($scope, fn);
    $rootScope.$safeApply(fn);
    

提交回复
热议问题