Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  64] [ 5]  / answers: 1 / hits: 30451  / 9 Years ago, mon, february 9, 2015, 12:00:00

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.





var myApp = angular.module('myApp', ['myApp.orders']);

myApp.constant('info', {
version : '1.0'
});

myApp.config(function(info) {
console.log('app config: ' + info.version);
});

myApp.controller('MyController', function (info) {
console.log('controller: ' + info.version);
});

var orders = angular.module('myApp.orders', []);

// Remove comments to see it fail.

//orders.config(function(info) {
// console.log('orders config: ' + info.version);
//});

<script src=https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js></script>

<div ng-app=myApp class=container ng-controller=MyController>
</div>





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


More From » angularjs

 Answers
70

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'])
...

[#67894] Friday, February 6, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
;