Javascript 错误:$digest 已在进行中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14838184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:05:45  来源:igfitidea点击:

Error: $digest already in progress

javascriptangularjsangularjs-directive

提问by iJade

I'm getting this error while trying to call

我在尝试拨打电话时收到此错误

        function MyCtrl1($scope, $location, $rootScope) {
      $scope.$on('$locationChangeStart', function (event, next, current) {
        event.preventDefault();
        var answer = confirm("Are you sure you want to leave this page?");
        if (answer) {
          $location.url($location.url(next).hash());
          $rootScope.$apply();
        }
      });
    }

MyCtrl1.$inject = ['$scope', '$location', '$rootScope'];

Error is

错误是

Error: $digest already in progress

回答by bmleite

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

重复:在调用 $scope.$apply() 时防止错误 $digest 已经在进行中

That error you are getting means Angular's dirty checking is already in progress.

你得到的那个错误意味着 Angular 的脏检查已经在进行中。

Most recent best practices say that we should use $timeoutif we want to execute any code in the next digestiteration:

最近的最佳实践表明,$timeout如果我们想在下一个摘要迭代中执行任何代码,我们应该使用:

$timeout(function() {
  // the code you want to run in the next digest
});


Previous response:(don't use this approach)

上一页反应:不要使用此方法

Use a safe apply, like this:

使用安全的应用程序,如下所示:

$rootScope.$$phase || $rootScope.$apply();

Why don't you invert the condition?

你为什么不反转条件?

$scope.$on('$locationChangeStart', function (event, next, current) {                
    if (confirm("Are you sure you want to leave this page?")) {
        event.preventDefault();
    }
});

回答by aw04

For others looking to troubleshoot this error, it's worth noting that the docsseem to suggest using the $timeoutservice to ensure the code will be called in a single $applyblock.

对于希望解决此错误的其他人,值得注意的是,文档似乎建议使用该$timeout服务来确保将在单个$apply块中调用代码。

$timeout(function() {
  $scope.someData = someData;
});

Also discussed in this questionif you look past the accepted answer.

如果您查看已接受的答案,也会在此问题中进行讨论。

回答by Hanmant Rachmale

Use

$scope.evalAsync(function(){ 
});

instead of

代替

$scope.$apply(function(){
});

回答by user2217057

josliber's answer solved a similar $digest problem that I was having. I just used

josliber 的回答解决了我遇到的类似 $digest 问题。我刚用

$scope.$evalAsync(function(){ 
   // code here
});

Good article here https://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm

这里的好文章 https://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm

回答by Hazarapet Tunanyan

Because your $scope.$apply() is inside AngularJs environment.In general I will not suggest you to use $apply(), moreover of $rootScope.$apply() functions make your application run slowly.It runs a new cycle of your whole scope.

因为你的 $scope.$apply() 是在AngularJs 环境里面的,一般我不建议你用 $apply(),而且 $rootScope.$apply() 函数会让你的应用程序运行缓慢。它运行一个新的循环你的整个范围。