I'm doing an angularjs project and encountered a problem:
I had a service for maintain user's login status. on the stage of the service get loaded, I want it could check whether the user is logged in, and load the user info if they are:
angular.module('tripod')
  .factory('UserService', ['$http', function($http) {
    var self = this,
      userId = angular.element(document.querySelector('#info-base')).attr('data-id');
    if (userId)
      $http.get('/session').then(function(response) {
        self.user = response.data;
      });
    return {
      user: self.user,
      signup: function(user) {
        return $http.post('/signup', user);
      },
      login: function(user) {
        var self = this;
        return $http.post('/login', user).then(function(response) {
          self.user = response.data;
          return self.user;
        });
      },
      logout: function() {
        return $http.get('/logout').then(function() {
          self.user = null;
        });
      },
      session: function() {
        var self = this;
        return $http.get('/session').then(function(response) {
          self.user = response.data;
          return response;
        });
      }
    };
  }]);
the data-id in the info-base is user's id, if they are logged in, the data-id will have value embedded, and their profile will reside in server's session, then I want get their profile through a http request. in factory's return value, I set a property point to the user info, and thought that I could use it later.
but when I refer UserService.user, I got undefined. I know the http promise won't return immediately, I tried set a long timeout, make sure the http promise returned. but the value of UserService.user still undefined.
And yeah, I did search around like this, but I just want know why the code above not working.
please someone teach me, thanks.
 
     
    