Recently, I came across some code that utilizes a global object to store angular services. These services are attached to the global object within the run function of the angular module. It made me wonder, could this approach potentially lead to issues in the future? How does it impact testing? On one hand, passing around services like this seems more convenient than injecting them into each controller individually, which may explain why it was implemented this way. However, what are the downsides to using this method? The following code snippet demonstrates the concept:
// Global variables
var globalObject =
{
ng: {},
};
// Module setup
var myModule = angular.module("myModule", []);
myModule.config(doStuff);
myModule.run(setUpGlobals);
// Setting up global app variables
function setUpGlobals(ngRootScope, ngHttp, ngTimeout)
{
globalObject.rootScope = ngRootScope;
// Angular services
globalObject.ng.http = ngHttp;
globalObject.ng.Timeout = ngTimeout;
}
setUpGlobals.$inject = ['$rootScope', '$http', '$timeout'];