Chapter 10 ■ Deployment ConsiDerations
This module isn’t going to be of much use to anyone unless it can be accessed. As it is declared in a separate file,
a script reference is needed. Also, we need to declare it as a dependency just as we would with any AngularJS module.
var app = angular.module('app', ['ngRoute', 'ngAnimate', 'app.config']);
Now it is just a matter of being able to access the myConfig constant so that we can access the configuration
properties that it contains. Listing 10-2 shows an example of just that.
Listing 10-2. Using the configuration data in the app.config module
module.factory('memberDataStoreService', function ($http, myConfig) {
var memberDataStore = {};
memberDataStore.doRegistration = function (theData) {
var promise = $http({method: 'POST', url: myConfig.apiUrl, data: theData});
return promise;
}
return memberDataStore;
})
Rather than hard code the value for the API URL, you can see here that we instead use the myConfig.apiUrl.
There are other, more sophisticated approaches to managing application configuration; your specific needs may vary
based on the kind of project on which you are working. We will look at a relatively common approach next.
A typical requirement is to have a set of configuration data for development and another set for production. Let’s
suppose that we have a locally installed database server with which our locally run development code communicates.
Of course, once our code is deployed to our production server, we then want it to communicate with our production
database. In this scenario, we have two sets of valid configuration data with which we could supply our app.
One way that we could handle this is to have two separate configuration modules—one for development and one
for production. Listing 10-3 shows a module that we can use during development.
Listing 10-3. app.development.config..js: our development configuration
angular.module('app.development.config',[])
.constant('myConfig', {
'database-host': '127.0.0.1',
'database-name: 'local-database'
});
You will notice that the myConfig object contains values specific to our locally installed database server.
Pay particular attention to the fact that the module is named app.development.config. Listing 10-4 shows its
counterpart: the production configuration module.
Listing 10-4. app.production.config.js: our development configuration
angular.module('app.production.config',[])
.constant('myConfig', {
'database-host': '168.63.165.103',
'database-name: 'production-database'
});