1

I have an infinite loop with angularjs $q and promise. This is the code:

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

This is the js code:

$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;
         };

And the error:

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) 

I don't have any watcher in my controller. What's happening?

2
  • Your code is not complete and we cannot analyze the problem properly without guessing. Can you create a demo on plnkr.co or jsfiddle.net? Commented Aug 13, 2014 at 10:32
  • Now its complete.I can not create a jsfiddle sorry. Commented Aug 13, 2014 at 10:34

1 Answer 1

1

An ng-hide expressions gets executed in every $digest loop. In the function in your ng-hide expression, you're pulling data from your database, which triggers a $digest. This causes the infinite $digest loop.

If you decouple the hiding and the data fetching this should work as intended.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.