javascript - 使用 $digest angularJS 进行无限循环

标签 javascript angularjs

我有一个带有 angularjs $q 和 Promise 的无限循环。 这是代码:

<a ng-hide="!isTechnician()" class="pull-right" href="#/admin/techniciansState"><span
                                        class="glyphicon glyphicon-map-marker"></span>&nbsp; Estado del técnico &nbsp;
                                </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) 

我的 Controller 中没有任何观察者。发生什么事了?

最佳答案

ng-hide 表达式在每个 $digest 循环中执行。在 ng-hide 表达式的函数中,您从数据库中提取数据,这会触发 $digest。这会导致无限的 $digest 循环。

如果您将隐藏和数据获取分离,这应该按预期工作。

关于javascript - 使用 $digest angularJS 进行无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25283778/

相关文章:

jquery - Angular Responsive Table 不适用于 Internet Explorer 9 (IE 9)。适用于 IE 10

javascript - 将 NodeJS express 对象传递给 AngularJS 1.6

javascript - 动态调用 Angularjs 指令

javascript - 如何在著名中使用 Transform.multiply 4x4?

javascript - Angular 选项选择显示隐藏 Angular 重复

javascript - 检测链接是否被用于访问页面

javascript - 如何与重叠形状的 d3.js 鼠标事件交互?

javascript - 多步 Angular ng-repeat 可提高性能

javascript - Angular 8 - @ViewChild 在父组件上返回未定义。 (没有嵌套的 ngIf 并在 ngAfterViewInit 中调用)

javascript - 是否可以创建ListItem的属性?