Inject constant to other modules config using Angular JS

martinoss picture martinoss · Feb 9, 2015 · Viewed 29.9k times · Source

I would like to share some variables like base paths throughout my application. These variables needs to be accessible during module configuration. My opinion was, that I can use a constant or provider for that.

I've got several modules and each one has it's own routing configuration. In these routing configurations, I want to access some settings for example.

This is working for the app-module-configuration but not for other module-configurations (for controllers on other modules it does), I always get "Unknown provider: info from myApp.orders".

I guess I have just missed out a little detail, do you have an idea?

Answer

ikumen picture ikumen · Feb 9, 2015

Your info constant is defined in your myApp module. If I understand your question correctly, you'd like to use the constants in other modules (e.g. myApp.orders module). If so, then you need to inject myApp into myApp.orders, but it looks like you want to do the reverse. One solution is to decouple the constants into a standalone module, and inject it as a dependency where needed.

angular.module('constants', []) 
  .constant(...);

angular.module('myApp', ['constants', 'myApp.orders'])
  ...

angular.module('myApp.orders', ['constants'])
  ...