Angularjs [$rootScope:inprog] 进度错误 [英] Angularjs [$rootScope:inprog] inprogress error
问题描述
我收到 angularjs [$rootScope:inprog] 错误.
I am getting angularjs [$rootScope:inprog] error.
错误:[$rootScope:inprog] http://errors.angularjs.org/1.2.7/$rootScope/inprog?p0=%24digest
.
这是函数调用
Members.get({}, function (response) { // success
$scope.family_mem = response.data;
}, function (error) { // ajax loading error
Data.errorMsg(); // display error notification
});
在控制台中,我通过 php 控制器函数获得结果.但不更新 $scope.family_mem
而是转到错误部分.这是指令
in console i am getting results by php controller function.but not updating $scope.family_mem
instead going to error part.
this is the directive
myApp.directive('mySelect', function() {
return{
restrict: 'A',
link: function(scope, element){
$(element).select2();
}
};
});
推荐答案
通常这意味着您在另一个已经具有生命周期的 Angular 代码中手动定义了 $rootScope.$apply.这在常见情况下不应该发生,因为 angular 会跟踪生命周期本身.需要它的一种常见情况是当您需要从非角度代码(如 jquery 或老式 js 内容)更新范围时.所以请检查你是否在某个地方有这个.如果您确实需要,最好使用安全应用(通用代码段):
Usually this means that you defined $rootScope.$apply somewhere manually inside of the another angular code which has lifecycle already. This should not be happen in common cases as angular tracks the lifecycle itself. The one common case where it is needed is when you need to update scope from non-angular code (like jquery or old-fashioned js stuff). So please check if you have this somewhere. In case you really need it's better to use safe apply (the common code snippet):
angular.module('main', []).service('scopeService', function() {
return {
safeApply: function ($scope, fn) {
var phase = $scope.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if (fn && typeof fn === 'function') {
fn();
}
} else {
$scope.$apply(fn);
}
},
};
});
然后您可以通过以下方式注入此服务并进行必要的调用:
Then you can inject this service and make necessary calling by:
scopeService.safeApply($rootScope, function() {
// you code here to apply the changes to the scope
});
这篇关于Angularjs [$rootScope:inprog] 进度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!