title: Angular .controller() runs before .run()
body: <p>I have an ajax call inside the .run() that loads a variable into the $rootScope
  That variable is needed in the controller associated with a view.</p>
  
  <p>Sometimes on refresh (F5) by the time the .controller is loading there is nothing inside $rootScope.user.fedUnit resulting in:</p>
  
  <p>TypeError: Cannot read property 'fedUnit' of undefined</p>
  
  <p>Any way to delay loading the controller until after the .run() is finished?
  Can't seem to find it.</p>
  
  <pre><code>app.run(function($rootScope, $http, $location, SessionFactory, TokenHandler) {
      token = TokenHandler.getToken();
      if ( token != null ) {
          SessionFactory.get( { token : token },
              function success(response, responseHeaders) {
                  $rootScope.user = response;
              }
          );
      }
  });
  
  app.controller('UnitController', function($scope, $rootScope, $location, UnitFactory) {
      $scope.updateUnits = function () {
          UnitFactory.query({fedUnit: $rootScope.user.fedUnit}, function success(response, responseHeaders) { ...
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <pre><code>$rootScope.foo = $q.defer();
  $rootScope.foo.resolve(); when AJAX is done;
  $rootScope.foo.promise.then(..) in the controller.
  </code></pre>
  
  <p>thanks to @misterhiller (twitter)</p>