0

In my app.config i need to retrieve a value from a service but i have an error message : UserServiceProvider.getUser is not a function. The goal is to allow the user to access some pages only if he is a part of a list.

Here's my service code :

var app =  angular.module('myApp');
  app.service('UserService', '$http', '$q', function(){
   userService.getUser = function () {
        var deferred = $q.defer();
        return $http.get(userUrl + "getUserName")
             .success(function (data) {
                 deferred.resolve(userService.userName = data);
                 console.log(data);
                 //alert('Success loading user');
             }).error(function (error) {
                 deferred.reject(error);
                 //alert('Error loading user' + error.message);
             })
        return deferred.promise;
    }
    return userService;
  });

My config :

  app.config(function(UserServiceProvider){
     UserServiceProvider.getUser()
      .then(function (data) {
          console.log(UserServiceProvider.userName);
      });
  });
user708683
  • 500
  • 3
  • 9
  • 26
  • 2
    Possible duplicate of [How to inject a service into app.config in AngularJS](http://stackoverflow.com/questions/22682753/how-to-inject-a-service-into-app-config-in-angularjs) – jsonmurphy Nov 09 '15 at 21:32
  • I read that post, but i cannot comment since i dont have enough point reputations. So i changed my initial question – user708683 Nov 10 '15 at 13:42
  • according to [this](http://stackoverflow.com/a/26408640/2556428). you should call `.$get()` on the provider first to get the instance, then call your method. I haven't tested it myself. – jsonmurphy Nov 10 '15 at 14:31
  • That works ! but when i try to display the userName in the index.html doing this : .state('/', { url: '/', templateUrl: 'index.html', controller: 'userCtrl', resolve: { user: function () { return {userName : userFromProvider}; } } }) It says : WARNING: Tried to load angular more than once. There a way to pass the userName without using state ? – user708683 Nov 10 '15 at 16:34
  • Is the purpose of injecting the service into `app.config` so that you can use it in your routes/state definitions? Because there is a better way to do this. – jsonmurphy Nov 10 '15 at 16:55
  • What is the best way then to pass the userName from my app.config to my index.html without using .state/resolve? Knowing that the $scope is not recognized in the app.config. I can create another controller to call a service, but that service will be called twice since it's already called in the app.config – user708683 Nov 10 '15 at 17:02
  • You can still use `.state/resolve`, but instead of trying to inject your service into `.config` you should actually put them in the resolve functions and they will be injected and instantiated there no problems. See [here](http://stackoverflow.com/questions/26623574/angular-ui-router-not-resolving-injected-parameters) for an example. – jsonmurphy Nov 10 '15 at 17:07
  • I have no choice to inject the UserProvider into .config because i load the states according to the user that i get from the service user. Once i retrieve this user from the service, i should display it in the index.html in the navbar. – user708683 Nov 10 '15 at 17:18
  • Hmmm...OK, not sure whats up with that warning... I guess you could close this open another question about the warning and try your luck from there. – jsonmurphy Nov 10 '15 at 18:21
  • there's a way to retrieve directly the value of the user in my app.config with something like this : UserProvider.$get().getUser() ? without using : UserProvider.$get().getUser() .then(function (data) { // }); – user708683 Nov 10 '15 at 21:08

0 Answers0