title: First AngularJS resolve not working
body: <p>The first time I visit a route with a resolve the request for the objects is not sent. The only way to visit the page is to make sure the route is correct in the url bar (typing or clicking a link) and refresh the page without caching (ctrl+shift+r in Firefox or ctrl+F5 in Chrome).</p>
  
  <p>After I visit it the first time link will work.</p>
  
  <pre><code>app.config(['$stateProvider', function($stateProvider){
  
    $stateProvider.state('users', {
      templateUrl: '/app/Users/templates/users.html',
      controller: 'Users',
      resolve: {
        'users': function(Objects, $stateParams){
          return Objects.getUsers();
        }
      },
      url: '^/users'
    });
  
    $stateProvider.state('user', {
      templateUrl: '/app/Users/templates/user.html',
      controller: 'User',
      resolve: {
        'user': function(Objects, $stateParams){
          return Objects.getUser($stateParams.id);
        }
      },
      url: '^/users/:id/'
    });
  }]);
  
  app.factory('Objects', ['$http', '$q', function($http, $q){
    /* Retrieve objects once */
    var _cache = {};
  
    function cache(key, promiseGetterFn) {
      if (key in _cache) {
        return _cache[key];
      }
      else {
        var promise = promiseGetterFn();
        _cache[key] = promise;
        return promise;
      }
    }
    return {
      unsetKey: function(key){
        delete _cache[key];
      },
      getUsers: function() {
        return cache('users', function () {
          var deferred = $q.defer();
          $http.get(HOST + '/api/v1.0/users/all').then(
            function (result) {
              deferred.resolve(result);
            });
          return deferred.promise;
        });
      },
      /*
      getUsers: function(){
        return cache('users', function(){
          return $http.get(HOST + '/api/v1.0/users/all').success(
            function(data, status, headers, config){
              return data.users;
            }
          );
        });
      },
      */
  /*
      getUsers: function(){
        return cache('users', function(){
          var deferred = $q.defer();
          return $http.get(HOST + '/api/v1.0/users/all').then(
            function(result){
              deferred.resolve(result.data.users);
            },
            function(status){
              deferred.reject(status);
            }
          );
          return deferred.promise;
        });
      },
  */
      getUser: function(id){
        return cache('user_' + id, function(){
          var deferred = $q.defer();
          return $http.get(HOST + '/api/v1.0/user/' + id).then(
            function(result){
              deferred.resolve(result.data.user);
            },
            function(status){
              deferred.reject(status);
            }
          );
          return deferred.promise;
        });
      },
    };
  }]);
  
  app.run(['$rootScope', '$location', 'LocalService', function($rootScope, $location, LocalService){
  
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
      if (!toState.publicAccess &amp;&amp; !LocalService.get('loggedIn')){
        /* Store the route they were trying to access */
        LocalService.set('next', $location.path());
  
        $location.path('/login');
      }
    });
  }]);
  </code></pre>
  
  <p><strong>Redirect after login code</strong></p>
  
  <pre><code>app.factory('AuthInterceptor', ['$q', '$injector', '$location', 'LocalService', function($q, $injector, $location, LocalService){
    /* Send Authorization in the header of each http request if there is a token */
    return {
      request: function(config){
        if (LocalService.get('token')){
          /* Using btoa to do Base64 */
          /* LocalService.password is only used on login to get token and will be empty ('') when using the token */
          config.headers.Authorization = 'Basic ' + btoa(LocalService.get('token') + ':' + LocalService.get('password'));
        }
        return config;
      },
      responseError: function(response){
        if(response.status === 401 || response.status === 403){
          /* Log the user out */
          LocalService.unset('loggedIn');
          LocalService.unset('token');
          LocalService.unset('user');
          $location.path('/login');
        }
        return $q.reject(response);
      }
    };
  }]);
  
  app.config(['$httpProvider', function($httpProvider){
    $httpProvider.interceptors.push('AuthInterceptor');
  }]);
  
  app.run(['$rootScope', '$location', 'LocalService', function($rootScope, $location, LocalService){
  
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
      if (!toState.publicAccess &amp;&amp; !LocalService.get('loggedIn')){
        /* Store the route they were trying to access */
        LocalService.set('next', $location.path());
  
        $location.path('/login');
      }
    });
  }]);
  
  app.controller('Login', ['$scope', '$http', '$location', 'growl', 'LocalService',
    function($scope, $http, $location, growl, LocalService){
      $scope.email = '';
      $scope.password = '';
  
      $scope.submitLogin = function submitLogin(){
        LocalService.set('token', $scope.email);
        LocalService.set('password', $scope.password);
        $http.get(HOST + '/api/v1.0/token').
          success(function(data, status, headers, config) {
            LocalService.set('token', data.token);
            LocalService.set('loggedIn', true);
            LocalService.set('password', '');
            /* Set current user */
            $http.get(HOST + '/api/v1.0/authenticate').then(function(result) {
              LocalService.set('user', JSON.stringify(result.data));
              if (LocalService.get('next')){
                var next = LocalService.get('next');
                LocalService.unset('next');
                console.log(next);
                $location.path(next);
              }
              else{
                $location.path('/');
              }
            });
          }
        ).
        error(function(data, status, headers, config) {
            /* invalid credentials growl */
            growl.addErrorMessage('Invalid username or password.');
          }
        );
      };
    }
  ]);
  </code></pre>