0

bootstrap.js

angular.element(document).ready(function() {
    var auth = angular.injector(['auth', 'ngStorage', 'ng']).get('auth');
    auth.user().then(function(user){
        if (user && user.id) {
            angular.bootstrap(document, ['app']);
        }
    });
});

auth.js

module.provider('auth', [function(){
    this.$get = ['$http', '$localStorage', function($http, $storage){
        return {
            _user: null,

            user: function(){
                var self = this;

                return $http.get('/auth/user').then(function(response){
                    self._user = response.data;
                    console.log(self._user); // <- has the user object
                    return self._user;
                });
            },
        }
    }]
}]);

app.js

module.run(function(auth, $state){
    console.log(auth._user); // <- is null
});

In my app, when i inject the auth service, seems like the status is lost - or, the injected auth is a a new instance (not a singleton).

Can anybody explain why?

brazorf
  • 1,943
  • 2
  • 32
  • 52

1 Answers1

1

Difference between Services, Factories and Providers: https://stackoverflow.com/a/15666049/1735789

Providers

Syntax: module.provider( 'providerName', function ); Result: When declaring providerName as an injectable argument you will be provided with (new ProviderFunction()).$get(). The constructor function is instantiated before the $get method is called - ProviderFunction is the function reference passed to module.provider.