使用$digest的无限循环
Infinite loop with $digest angularJS
我有一个angularjs $q和promise的无限循环。这是代码:
<a ng-hide="!isTechnician()" class="pull-right" href="#/admin/techniciansState"><span
class="glyphicon glyphicon-map-marker"></span> Estado del técnico
</a>
这是js代码:
$scope.isTechnician = function () {
if (!$scope.notCheckTechnician) {
SecurityService.getCurrentUser().then(function (user) {
if ($.inArray('technician', user.roles)) {
return true;
} else {
return false;
}
});
}
};
var SecurityService = function($resource, $q, $rootScope, API_URL) {
// Definición del servicio REST
var Security = $resource(API_URL + '/security/:action', {
action: "@action"
}, {
'currentUser': {
method: 'GET',
isArray: true,
params: {
action: 'current-user'
}
}};
var getCurrentUser = function() {
var deferred = $q.defer();
var user = Security.currentUser(function() {
if (user.length > 0) {
_setCurrentUser(user[0]);
deferred.resolve(user[0]);
} else {
_setCurrentUser(null);
deferred.reject('Not authenticated');
}
}, function() {
_setCurrentUser(null);
deferred.reject('Not authenticated');
});
return deferred.promise;
};
和错误:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.2.18/$rootScope/infdig?p0=10&p1=%5B%5D
at http://localhost:900/bower_components/angular/angular.js:78:12
at Scope.$get.Scope.$digest (http://localhost:900/bower_components/angular/angular.js:12434:19)
at Scope.ng.config.$provide.decorator.$delegate.__proto__.$digest (<anonymous>:844:31)
at Scope.$get.Scope.$apply (http://localhost:900/bower_components/angular/angular.js:12660:24)
at Scope.ng.config.$provide.decorator.$delegate.__proto__.$apply (<anonymous>:855:30)
at done (http://localhost:900/bower_components/angular/angular.js:8272:45)
at completeRequest (http://localhost:900/bower_components/angular/angular.js:8477:7)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:900/bower_components/angular/angular.js:8416:11)
我的控制器中没有任何监视器。发生什么事情了?
在每个$digest循环中执行ng-hide
表达式。在ng-hide
表达式中的函数中,您正在从数据库中提取数据,这会触发$digest。这会导致$digest无限循环。
如果你解耦了隐藏和数据获取,这应该是预期的工作
相关文章:
- 使用滚动溢出-x进行无限循环
- 从index.html调用函数,该函数无限循环
- 使用jQuery无限循环播放HTML页面幻灯片
- Grunt任务没有加载,获得无限循环
- 如何在不进入无限循环的情况下将网站重定向到Facebook画布URL
- Backbone+RequireJS+Mediator模式导致视图逻辑短路和无限循环
- jQuery无限循环,动画化许多项目
- 如何避免试图用php+jquery显示php起始页的无限循环
- 6502仿真增强型基本无限循环$C000至$E0ED
- Angular ngRoute导致无限循环和堆栈溢出
- Jquery:无限循环和暂停
- 数组数组:无限循环
- MeteorJS使用流星调用和流星方法时的无限循环
- 如何无限循环数据集字符串
- jquery在无限循环中运行
- 遍历对象会导致无限循环
- 为什么内部Javascript循环会阻止外部循环中断(即无限循环)
- AngularJS v1.2.x $digest() 无限循环指令隔离范围对象参数
- 使用$digest的无限循环
- 调用$watchCollection时的angular$digest无限循环