错误:$digest 已在进行中 [英] Error: $digest already in progress
本文介绍了错误:$digest 已在进行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试调用
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: $digest already in progress
推荐答案
重复:在调用 $scope.$apply() 时防止错误 $digest already in progress
你得到的那个错误意味着 Angular 的脏检查已经在进行中.
That error you are getting means Angular's dirty checking is already in progress.
最近的最佳实践表明,如果我们想在下一次摘要迭代中执行任何代码,我们应该使用 $timeout
:
Most recent best practices say that we should use $timeout
if we want to execute any code in the next digest iteration:
$timeout(function() {
// the code you want to run in the next digest
});
<小时>
之前的回复:(不要使用这个方法)
使用安全的应用程序,如下所示:
Use a safe apply, like this:
$rootScope.$$phase || $rootScope.$apply();
为什么不反转条件?
$scope.$on('$locationChangeStart', function (event, next, current) {
if (confirm("Are you sure you want to leave this page?")) {
event.preventDefault();
}
});
这篇关于错误:$digest 已在进行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文