Skip to content

Instantly share code, notes, and snippets.

@BenArunski
Created July 13, 2016 17:45
Show Gist options
  • Save BenArunski/1281d0dcb1fdbfab1f08cf7d16666c97 to your computer and use it in GitHub Desktop.
Save BenArunski/1281d0dcb1fdbfab1f08cf7d16666c97 to your computer and use it in GitHub Desktop.
Angular Environment Bootstrap with ui-router
<html>
<body>
...
<script>
(function() {
'use strict';
var cb = function (cfg) {
angular.module('app.config', []).constant('CONFIG', cfg);
angular.element(document).ready(function() {
// instead of <html ng-app="main">
angular.bootstrap(document, ['main']);
});
};
angular.injector(['ng']).get('$http').get('/other-app-on-tomcat/config.json').then(
function(res){ cb(res.data); },
function(reason){ cb({failed: reason}) }
);
})();
</script>
</body>
</html>
(function () {
'use strict';
angular
.module('main')
.config(config);
/** @ngInject */
function config($stateProvider) {
$stateProvider
.state('app', {
abstract: true,
resolve: {
checkConfig: checkConfig
}
}
);
/* @ngInject */
function checkConfig($q, CONFIG, $log) {
var d = $q.defer();
var routeKiller = d.promise;
if(CONFIG.failed) {
cancelRouteAndMessage(CONFIG.failed);
} else {
success();
}
function cancelRouteAndMessage(reason) {
$log.error('Is other-app-on-tomcat running? There was a problem fetching the config because of the error:');
$log.error(reason);
d.reject('There was a problem fetching the config');
}
function success() {
d.resolve('config is loaded and verified');
}
return routeKiller;
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment