错误:$消化正在进行中 [英] Error: $digest already in progress
问题描述
我试图打电话收到此错误
函数MyCtrl1($范围,$位置$ rootScope){
$范围。在('$ locationChangeStart',函数(事件,接下来,电流){$
。事件preventDefault();
VAR答案=确认(你确定要离开这个页面吗?);
如果(回答){
$ location.url($ location.url(下).hash());
$ rootScope $适用()。
}
});
}MyCtrl1 $注入='$范围,$位置,$ rootScope']。
错误是
错误:$消化正在进行中
重复的:<一href=\"http://stackoverflow.com/questions/12729122/$p$pvent-error-digest-already-in-progress-when-calling-scope-apply\">$p$pvent错误调用$范围在$已消化进度。$适用()
您获得的错误意味着角的脏检查正在进行中。
最近的最佳做法,说我们应该用 $超时
如果我们想在执行任何code接下来的消化的迭代:
$超时(函数(){
//将code要在接下来的运行摘要
});
previous响应:(不要使用这种方法)
使用一个安全的应用,如:
$ rootScope。$$阶段|| $ rootScope $适用()。
你为什么不反转的条件?
$范围。在$('$ locationChangeStart',函数(事件,接下来,电流){
如果(确认(你确定要离开这个页面吗?)){
。事件preventDefault();
}
});
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
Duplicated: Prevent error $digest already in progress when calling $scope.$apply()
That error you are getting means Angular's dirty checking is already in progress.
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
});
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();
}
});
这篇关于错误:$消化正在进行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!