Angular 常量:配置

Angular constant : config

我正在开发一个 Angular 应用程序,我想创建一个配置文件。根据我的阅读,我应该使用 angular 常量。

所以我尝试创建我的常量。从我读到的内容 (here, here and also + doc) 来看,这似乎很简单。

这是我的文件:

 angular.module('app')

     .constant('config', {

         routers : {

             Commandes: 'some_data',
             Prestations: 'some_data',
             Interventions: 'some_data'
        },

         serverPath : 'some_data'
 });

当我读到可以将它注入服务时,我尝试在工厂中使用它并显示它:

 app.factory('myFactory', [function(config) { 

      console.log('config : '+config);
      return // something;
}]);

但是配置出现"undefined"(这里没有错误,只是console.log东西)。

我好像没听懂,但我到底错过了什么?

要从工厂服务中检索常量,您必须使用您在配置文件中定义的整个路径。

在你的例子中:

config.serverPath
config.routers.Commandes
// etc...

另一件事是您需要将常量定义为方括号内的依赖项:

app.factory('myFactory', ['config', function(config) { 

      console.log('config : '+config);
      return // something;
}]);