Skip to content

Instantly share code, notes, and snippets.

@Kikketer
Created January 27, 2015 16:58
Show Gist options
  • Save Kikketer/e6f4f7f7b8626f7c2005 to your computer and use it in GitHub Desktop.
Save Kikketer/e6f4f7f7b8626f7c2005 to your computer and use it in GitHub Desktop.
Synchronous Environment Injection
'use strict';
angular.module('yourModule')
.factory('environmentService', function() {
var environment;
return {
getEnvironment: function() {
// We need this to be synchronous since everything depends on it.
// If we return a promise, there is a cascading change (not fun)
if (!environment) {
$.ajax('environment', {async: false})
.success(function(env) {
environment = env;
return environment;
})
.error(function() {
throw 'Environment variables not set';
});
}
return environment;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment