Javascript 找不到AngularJS配置模块

Javascript 找不到AngularJS配置模块,javascript,angularjs,Javascript,Angularjs,我对AngularJS框架相当陌生,但基本上我正在尝试向我的应用程序中注入CSRF令牌,但我想根据配置更改url。以下是我到目前为止的情况: var VERISION_API = 'v1'; var config_data = { 'CFG': { 'EP': 'https://mydomain.com/api/' + VERISION_API + '/web/' } }; var configMod = angular.module("cfg",[]); ang

我对AngularJS框架相当陌生,但基本上我正在尝试向我的应用程序中注入CSRF令牌,但我想根据配置更改url。以下是我到目前为止的情况:

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']);
            });
        });
    }]);
})();
我不断得到以下错误:


未捕获错误:[$injector:unpr]未知提供程序:cfgProviderangular通过精确区分大小写的字符串键从
providerCache
获取服务,因此应使用
CFG
两个问题,请参阅内联:-

  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']);
            });
        });
    }]);
2) DI服务/提供商/等。。名称区分大小写,因此:

  $injector.invoke(['CFG',...
3) 不要在
$rootScope的匿名函数中传递参数。$apply
它将在该范围内创建一个局部变量。所以只要:

  $rootScope.$apply(function () {
注入的依赖项可以作为变量(argument
cfg
)从上层作用域中获得,因此只需通过以下方式访问它:

   $http.get(cfg.EP + "accounts/csrf");
检查演示中的网络控制台:

var configMod=angular.module(“cfg”,[]);
变量配置_数据={
“CFG”:{
‘EP’:’https://mydomain.com/api//web/'
}
};
var configMod=angular.module(“cfg”,[]);
angular.forEach(配置数据、函数(键、值){
configMod.常量(值、键);
});
var myApp=angular.module(“app”、[“cfg”、“ngResource”、“ngRoute”]);
(功能(){

var$injector=angular.injector(['ng','cfg']);//可能区分大小写,尝试将
'CFG'
作为值注入
$injector
@davintroon这不仅仅是问题所在……您还需要在injector中插入模块。@PSL啊,是的,错过了。坦率地说,我从未见过这样做。我想我更喜欢http拦截器。@davintroon哦,是的,这是一个很好的观点。。事实上,我甚至没有看到它在做什么。加上
cfg
,因为它是应用程序依赖项列表的一部分
cfg
将只在模块
app
中可访问,令牌可以通过拦截器添加,甚至可以添加。但不确定OP在真正的代码段中到底在做什么。
   $http.get(cfg.EP + "accounts/csrf");