I would say that the strategy is changing, depending on what configuration you have, but you have several options:
Full module constants
If you need only a few constants, you can use .value() , for example:
var app; app = angular.module("my.angular.module", []); app.value("baseUrl", "http://myrestservice.com/api/v1"); //injecting the value app.controller("MyCtrl", ['baseUrl', function (baseUrl) { console.log(baseUrl); // => "http://myrestservice.com/api/v1" }]);
See a more detailed answer here .
Getting config / config service
What I personally like to do is get my configuration from another place through the service, as usual. It does not matter if it is a remote location or just static information.
var app; app = angular.module("my.angular.config", []); app.service('Configuration', [function() { return { getbaseUrl: function() { return "http://myrestservice.com/api/v1" }, getConfig: function() { return { answer: 42, question: "??" } } } }]):
EDIT: an example with external sampling:
var app; app = angular.module('my.module.config', []); app.factory('ConfigurationData', ['$http', '$q', function(http, q) { var deferredConfig = q.defer(); //error handling ommited http.get('http://remote.local/config.json').success(function(data) { return deferredConfig.resolve(data); }); return { getConfig: function() { return deferredConfig.promise; } }; }]);
Using this service, you can enter your config in other services, however, you may encounter synchronization problems, since you need to enter and resolve the promise made by the service before what you want to do with the configuration:
var app; app = angular.module("my.other.module", ['my.module.config']); app.factory('MyAwesomeService', ['ConfigurationData', function(config) { config.getConfig().then(function(config) {
Here you get a little more subtle control, as you can respond to different inputs. Again, it depends on your use case. You can use factory here if you need additional logic to structure your configuration.
Finally, if you want even more control over the configuration, you can do
Custom provider
Providers can be very useful, but I think they are a little harder to develop. Given baseUrl from the configuration necessary for your application to work, you can write to the provider for a service that needs a baseUrl value, for example:
var app; app = angular.module('my.angular.module', []); app.provider("RestServiceProvider", function(){ this.baseUrl = 'http://restservice.com'; this.$get = function() { var baseUrl = this.baseUrl; return { getBaseUrl: function() { return this.baseUrl; } } }; this.setBaseUrl = function(url) { this.baseUrl = url; }; });
This allows you to do cool things in the configuration phase of your application:
app.config(['RestserviceProvider', function(restServiceProvider) { restServiceProvider.setBaseUrl('http://wwww.myotherrestservice.com'); }]);
Every instance that you get in the service / controller / etc. RestService will now have a baseUrl set from the configuration phase from the moment it is entered.
For a more in-depth review, I suggest gist .