1

So I have this service :

var module = angular.module('app.shared.user', []);

module.service('userService', ['$q', '$http', 'firebaseService', function($q, $http, firebaseService) {
    this.isAdmin = undefined;
    this.userIdToken = undefined;
    let defered = $q.defer();
    this.promise = defered.promise;

    this.setIdToken = function (token) {
        this.userIdToken = token;
        defered.resolve(token);
        console.log("resolved in user service");
    };}])

I'd like to use it in my app config. I tried what's proposed here but the thing is, this method creates another instance of my service as a Provider.

Therefore, when setIdToken() is called in my controller, my promise is resolved in one instance. But when I wait for it in my app config, it's never resolved (other instance of the service, so other instance of the variables).

So my aim is, to use this service as a service in my controller, that calls a method that resolves a promise which is declared in my service, and wait the resolution of this same promise in my app config.

This is what I also tried in my app config, but unsuccessfully :

    function config(userService) {
            userService.promise.then(function (token) {
                console.log("token", token);
            });
        }

  config.$inject = ["userServiceProvider"];
  app.config(config);

Any ideas ? Thanks in advance

  • 1
    You **have** to register your userService as a Provider, with it's own $get() method, to use it the way you are intending. The bigger question is, exactly what is it you are trying to do? – Steve -Cutter- Blades Oct 24 '18 at 15:58
  • Thanks for your answer. So my controller is going to call the method `setIdToken` at the initialization of the app. This method sets a variable with a token generated when a user is connected and resolves a promise. My goal is, in my app config, wait for this promise to be resolved, and when it is, make an API call (can't be done before a user is connected to the app). Also, I tried to "convert" my service into a provider with the method used in the link I shared, but unsuccessfully (service methods didn't seem to be found). – Stéphane Conq Oct 25 '18 at 06:43
  • Still confused. When app.config() is running you are initializing your app, and you have a connected user (they might need to login or something, after the app is initialized, but the user opened the page). If you write your service/provider right, you call your `setTokenId` in your config and on response set some app level variable right in your `config()` method. (We write ours to localStorage instead, but everyone does this different...) – Steve -Cutter- Blades Oct 25 '18 at 11:05
  • The config methods are meant to create bits necessary to run your application (constants, routing, global app level variables). You may want to consider placing user session type stuff in a session, instead of directly in the app (localStorage, cookie, etc) – Steve -Cutter- Blades Oct 25 '18 at 11:07
  • What I call initialize the app is in fact, in the controller, getting the connected user (we use connections via Google accounts, and user is already connected when he arrives on the page) and fetching all the data I need to display it in my page. My case in more details is basically fetching data to translate the app. The way it's done now, the user has to be connected to fetch this data. If ever I do as you say and "initialize" my app in app.config(), how do I send it back to my controller (that's where I really need it) ? – Stéphane Conq Oct 25 '18 at 12:43
  • If you store the token in localStorage, then you can query localStorage from your controller to 'get' the token. We have a provider that maintains a registry of endpoint urls. Calling a method on the provider, in module configs, we populate that registry (maintained in the provider/service). Since services are singletons, any time we include that 'service' in a controller, we can access the registry to get an endpoint value. (all of this is a little easier when using es classes) – Steve -Cutter- Blades Oct 25 '18 at 12:53
  • The problem is the same eitherway because I'll have to make an API call to fetch the token at least for the first time. My problem is making my controller wait until this token exists before I make an API call. So your method will work once the token is in localStorage (querying localStorage should be faster than an API call), but it won't work if there's nothing in localStorage. Plus, I think it's a bit wobbly to play on timings like that and I really need to use my service as a service in my controller and as a provider in my app.config(). – Stéphane Conq Oct 25 '18 at 13:31

0 Answers0