Search code examples
angularjsrootscopeangular-local-storage

angularjs - localstorage not loading properly


I'm working on a little web app for a school project, I store user data on localstorage like this:

UserService.login($scope.user).then(function (response){
    if(response.data.Error){
        $scope.credentialsError = true;
    }else{
        $rootScope.cliente = response.data.result;
        localStorageService.set('cliente', $rootScope.cliente);
        $location.path('/dashboard');
    }
});

As long as I remain in the page, everything works fine, the user is still in $rootScope doing it's thing. The problem comes when I reload. Upon startup i run this:

angular.module('app-gp').run([
  "$rootScope",  "localStorageService", "$state", function($state,localStorageService, $rootScope) {
      $rootScope.cliente = localStorageService.get('cliente');
}]);

Which appears to correctly get the user data from local storage. However when I request $rootScope.cliente from a controller or a template I get undefined.

Thanks for your help.


Solution

  • After a while i managed to solve the problem. It seems it was never about the localStorageservice, what i did was rearrange this:

    angular.module('app-gp').run(["$rootScope", "localStorageService", "$state", function($rootScope, localStorageService, $state) {
    

    and it worked...