Arhyaa
Arhyaa

Reputation: 369

Angular constant : config

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 ?

Upvotes: 1

Views: 307

Answers (1)

Endre Simo
Endre Simo

Reputation: 11551

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;
}]);

Upvotes: 1

Related Questions