Stéphane Conq
Stéphane Conq

Reputation: 48

Use service as provider in config but also as service in controller

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

Upvotes: 1

Views: 49

Answers (0)

Related Questions