1

I'm fairly new to the AngularJS framework, but basically what I am trying to do is inject a CSRF token into my app, but I want to change the url based on a config. Here is what I have so far:

var VERISION_API = 'v1';
var config_data = {
    'CFG': {
        'EP': 'https://mydomain.com/api/' + VERISION_API + '/web/'
    }
};
var configMod = angular.module("cfg",[]);
angular.forEach(config_data,function(key,value) {
  configMod.constant(value,key);
});

var myApp = angular.module("app", ["cfg", "ngResource", "ngRoute"]);

(function () {
    var $injector = angular.injector(['ng']);
    $injector.invoke(['cfg', '$http', '$rootScope', function (cfg, $http, $rootScope) {
        $rootScope.$apply(function (CFG) {
            $http.get(CFG.EP + "accounts/csrf").then(function (response) {
                myApp.constant("CSRF_TOKEN", response.csrf_token);
                angular.bootstrap(document, ['app']);
            });
        });
    }]);
})();

I keep getting the following error:

Uncaught Error: [$injector:unpr] Unknown provider: cfgProvider <- cfg

I know it has something to do with the way that I am running the $injector.invoke, but I have tried everything. Hopefully someone could help me out and tell me what I am doing wrong?

4
  • Might be case sensitive, try 'CFG' as a value to inject into $injector. Commented Jan 6, 2015 at 16:55
  • @DavinTryon That is not just the issue though... You need the module in the injector as well. Commented Jan 6, 2015 at 17:08
  • 1
    @PSL ah yeah, missed that. Frankly, I've never seen it done like this. I'd prefer an http interceptor, I think. Commented Jan 6, 2015 at 17:11
  • @DavinTryon oh yeah that is a good point.. I actually did not even see what is it doing. Plus cfg since it is a part of app's dependency list CFG will just be accessibly in the module app, and the token can just be adden via the interceptor or it can even be added like this. But not sure is that is exactly what OP is doing in the real piece of code. Commented Jan 6, 2015 at 17:14

2 Answers 2

2

Couple of issues, See inline:-

  var $injector = angular.injector(['ng', 'cfg']); //<-- Add module name here
   /*Injection is case sensitive it mustbe CFG*/
    $injector.invoke(['CFG', '$http', '$rootScope', function (cfg, $http, $rootScope) {
        $rootScope.$apply(function () { //Do not set an argument here
            $http.get(cfg.EP + "accounts/csrf").then(function (response) {
                myApp.constant("CSRF_TOKEN", response.csrf_token);
                angular.bootstrap(document, ['app']);
            });
        });
    }]);

1) You need to get the injector with the module that has the dependency, example:

 var $injector = angular.injector(['ng', 'cfg']);

2) DI service/provider/etc.. names are case sensitive so:

  $injector.invoke(['CFG',...

3) Do not pass an argument in the anonymous function of $rootScope.$apply it will create a local variable within that scope. So just:

  $rootScope.$apply(function () {

injected dependency is available as variable (argument cfg) from the upper scope, so just access it as:

   $http.get(cfg.EP + "accounts/csrf");

Check the network console in the demo:

var configMod = angular.module("cfg", []);
var config_data = {
  'CFG': {
    'EP': 'https://mydomain.com/api//web/'
  }
};
var configMod = angular.module("cfg", []);
angular.forEach(config_data, function(key, value) {
  configMod.constant(value, key);
});
var myApp = angular.module("app", ["cfg", "ngResource", "ngRoute"]);

(function() {
  var $injector = angular.injector(['ng', 'cfg']); //<-- Add module name here
  /*Injection is case sensitive it mustbe CFG*/
  $injector.invoke(['CFG', '$http', '$rootScope',
    function(cfg, $http, $rootScope) {
      $rootScope.$apply(function() { //Do not set an argument here
        $http.get(cfg.EP + "accounts/csrf").then(function(response) {
          myApp.constant("CSRF_TOKEN", response.csrf_token);
          angular.bootstrap(document, ['app']);
        });
      });
    }
  ]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

angular gets service from providerCache by exact the string key case sensitive, so CFG should be used

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.