I. Black I. Black - 1 year ago 85
AngularJS Question

Cannot acces variable from angular provider inside angular config

I'm new to angular, so I'm pretty sure it is a stupid question, but I failed to figure this out myself.
I have an app in which I have provider to get token from cookies using ngCookies, and I need to access it from inside app.config. I've tried several methods, but all I can get is service code, not a returned variable. What am I doing wrong?

var app = angular.module('appClient', [

app.provider('getToken', function getTokenProvider() {
this.$get = ['getTokenService', function(getTokenService, $cookies) {
var token = $cookies.get('token');
return token;

app.config(function ($resourceProvider, $httpProvider, getTokenProvider) {
var token = ? //need to get token from getTokenProvider here
$httpProvider.defaults.withCredentials = false;
$resourceProvider.defaults.actions = {
save: { method: 'POST', params: { token: token } },
get: { params: { token: token } },
query: { params: { token: token }, isArray: true }

Answer Source

Get a tricky solution based on your code. Anyone tell me if this has any side effects or drawbacks.

app.provider('getToken', function getTokenProvider() {

        this.$get = ['$cookies', function($cookies) {
        var token = $cookies.get('token');
        return token;


    app.config(function (getTokenProvider,$injectorProvider) {
      //instantiate or get the service here
      //this should be how angular gets services
      //$injectorProvider.$get returns the instance injector (the $injector service)
      var token = $injectorProvider.$get().get("getToken");


Here is the plunker


Using this may cause problem when the there is any code changing the provider of depended services used to instantiate 'getToken'. For example, if you have module.decorate for ngCookies after this config block, it will fail to work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download