Reputation: 203
Does anyone know the best way to create and access a global constant is AngularJS?
I want to be able to doe something like:
app.constant('baseURL','http://www.baseurl.com');
and access it from any factory or controller.
Upvotes: 3
Views: 2595
Reputation: 136144
Going for constant would be one of the better way as it has ability that we can access it inside the config
level or in provider
, so that we can take use of them to carry the config level settings.
For usage you could inject the same way as we do inject for service/factory
or consider any dependency.
For making it more better you could use Object inside the your app.constant
, so that it can carry multiple settings in it.
Constant
app.constant('configSettings', {
'baseUrl': 'http://www.baseurl.com',
'someElseSetting': 'settingValue'
//other setting will also be there.
});
Controller
app.controller('myCtrl', function($scope, configSettings){
//console.log(configSettings.baseUrl);
//you could use base URL here
});
Upvotes: 5