1

I'm working on an Angular app and I'd like to create a config file. From what I read, I should use angular constant.

So I tried to create my constant. From what I read (here, here and also here + doc), it seems quiet easy.

Here is my file :

 angular.module('app')

     .constant('config', {

         routers : {

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

         serverPath : 'some_data'
 });

As I read it's possible to inject it in a service, I tried to use it in a factory and to display it :

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

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

But the config appears "undefined" (no error here, just console.log things).

It seems that I'm not gettong it, but what the hell did I miss ?

Community
  • 1
  • 1
Arhyaa
  • 369
  • 1
  • 3
  • 21

1 Answers1

1

To retrieve the constant from the factory service you have to use the whole path as you defined in your config file.

In your example:

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

Another thing is that you need to define the constants as dependencies inside the square brackets:

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

      console.log('config : '+config);
      return // something;
}]);
Endre Simo
  • 11,330
  • 2
  • 40
  • 49
  • I tried to use 'config' in the factory because I was getting an error when using 'config.serverPath', as I forgot to define the constant as a dependecy. Thanks a lot man ! – Arhyaa Jun 25 '15 at 08:37