title: solving $rootScope:infdig Infinite $digest Loop
body: <p>I get the basic idea of the infinite digest loop and how it happens, but I'm running into the problem. Here is a fiddle demonstrating my code and problem:</p>
  
  <p><a href="http://jsfiddle.net/eS5e5/1/" rel="noreferrer">http://jsfiddle.net/eS5e5/1/</a></p>
  
  <p>In the jsfiddle console you'll see the infinite digest loop.</p>
  
  <p>Basically I have to make decisions on data that may not have loaded yet so I need to wait for the promise to resolve using then(). I have a promise called user. There are two different places in the code where I call then() on user.</p>
  
  <ol>
  <li>Just after I define it. I need to set a scope variable based on it.</li>
  <li>In another scope method, $scope.isAdmin()</li>
  </ol>
  
  <p>For number 2, it might be asked why I just don't use $scope.user directly in the $scope.isAdmin() method. The problem is, it's possible for $scope.isAdmin() to be called before the async request for the user returns, in which case I need to 'block' before returning from $scope.isAdmin().</p>
  
  <p>My question is, what about $scope.isAdmin() is making angular think that a 'watched' variable has changed and that the digest cycle needs to run again?</p>
  
  <p>$scope.isAdmin() isn't actually changing anything.</p>
  
  <p>Here is the stripped down code:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;body ng-controller='myController'&gt;  
    &lt;div ng-if='isAdmin()'&gt;Hi! &lt;strong&gt;{{ user.username }}&lt;/strong&gt; is an Admin!!!&lt;/div&gt;
    &lt;div ng-if='!isAdmin()'&gt;Hi! &lt;strong&gt;{{ user.username }}&lt;/strong&gt; is NOT an Admin!!!&lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>And the JS:</p>
  
  <pre><code>angular.module('myApp', [])
    .factory('myService', function($q, $timeout) {
      return {        
        getUser: function() {
          var deferred = $q.defer();
  
          $timeout(function() {
            deferred.resolve({ username: 'me', isAdmin: true });
          }, 2000);
  
          return deferred.promise;
        }
      };
    })
    .controller('myController', function($scope, $q, myService) {      
      var getUserDeferred = $q.defer();
      var user = getUserDeferred.promise;
      user.then(function(user) {
        $scope.user = user;
        return user;
      });
  
      $scope.getUser = function() {
        return myService.getUser().then(function(user) {
          getUserDeferred.resolve(user);
        });
      };
  
      $scope.isAdmin = function() {
        return user.then(function(user) {
          return user.isAdmin;
        });
      };
  
      $scope.getUser();
    });
  </code></pre>