title: Service: Init function doesn't initiate data
body: <p>I have an app built with Ionic, where I list articles and on opening a single article I initiate user data with the function <code>init</code> in <code>UserService</code>:</p>
  
  <pre><code>angular.module('coop.services')
  .factory('UserService', function(
    $http,
    AppSettings,
    $rootScope,
    $auth,
    $q,
    $resource,
    $cordovaFileTransfer
  ) {
    var service = {
      user: {},
  
      init: function() {
        var user = localStorage.getItem('coop_user');
  
        if (user) {
          service.user = JSON.parse(user);
        }
  
        service.engagementResource = $resource(
          AppSettings.apiUrl + '/user/:userId/engagements',
          {
            userId: service.user.id
          }
        );
      },
  
      set: function(user) {
        service.user = user;
        localStorage.setItem('coop_user', JSON.stringify(user));
      },
  
      get: function() {
        return service.user;
      },
  
      getProfilePicture: function(user, size) {
        return user &amp;&amp; user.photo
          ? AppSettings.mediaServer + '/imagecache/user' + size + '/users/' + user.photo
          : 'images/pic.jpeg';
      },
  
      clear: function() {
        service.user = {};
        localStorage.removeItem('coop_user');
      },
  
      sendContactConfirmation: function(contact, reset) {
        var defer = $q.defer();
  
        if(reset == 'reset'){
          var endpointUrl = $http.post(AppSettings.apiUrl + "/users/reset", { phone: contact });
        }
        else {
          var endpointUrl = $http.post(AppSettings.apiUrl + "/users", { phone: contact });
        }
  
        endpointUrl.then(function(result) {
          service.set(result.data.user);
          defer.resolve(result);
        }, function(error) {
          defer.reject(error);
        });
  
        return defer.promise;
      },
  
      sendActivation: function(userId, code, form) {
        var defer = $q.defer();
  
        $http.put(AppSettings.apiUrl + '/users/' + userId + '/activate', { code: code }).then(function(result) {
          if (form == 'register'){
            $auth.setToken(result.data.token);
          }
  
          defer.resolve(result);
        }, function(error) {
          defer.reject(error);
        });
  
        return defer.promise;
      },
  
      resetPassword: function(userId, password) {
        var defer = $q.defer();
  
        $http.put(AppSettings.apiUrl + '/users/' + userId + '/reset', { password: password }).then(function(result) {
          service.set(result.data.user);
  
          defer.resolve(result);
        }, function(error) {
          defer.reject(error);
        });
  
        return defer.promise;
      },
  
      save: function(user) {
        var defer = $q.defer();
  
        $http.put(AppSettings.apiUrl + '/users/' + user.id, user).then(function(result) {
          $auth.setToken(result.data.token);
          service.set(result.data.user);
  
          defer.resolve(result);
        }, function(error) {
          defer.reject(error);
        });
  
        return defer.promise;
      },
  
      saveProfilePicture: function(pictureUrl) {
        var defer = $q.defer();
  
        $cordovaFileTransfer.upload(
          AppSettings.apiUrl + '/users/' + service.user.id + '/pic',
          pictureUrl,
          {
            headers: {
              'Accept': 'application/json',
              'Authorization': 'Bearer ' + $auth.getToken()
            }
          },
          true
        ).then(function(result) {
          var user = JSON.parse(result.response).user;
          service.set(user);
  
          defer.resolve(user);
        }, function(error) {
          defer.reject(error);
        });
  
        return defer.promise;
      },
  
      join: function(engagementId){
        return service.engagementResource.save({
          engagementId: engagementId
        });
      },
  
      leave: function(engagementId) {
        return service.engagementResource.delete({
          engagementId: engagementId
        });
      },
  
      getEngagements: function() {
        return service.engagementResource.query();
      },
    };
  
    service.init();
  
    return service;
  });
  </code></pre>
  
  <p>But when I open the single article page with a new user, I don't get any data for the user, I get <code>null</code> in the console.</p>
  
  <pre><code>init: function() {
        var user = localStorage.getItem('coop_user');
         console.log(user);
  </code></pre>
  
  <p>And, then my requests to the backend fail, since I am not sending user data. On the login, I am setting user data to the <code>localStorage</code> like this in the <code>auth controller</code> like this:</p>
  
  <pre><code>$scope.loginPromise.then(function(response) {
          UserService.set(response.data.user);
  </code></pre>
  
  <p>If I refresh the page then I get the data for the user, but not before that. How can I fix that?</p>