Skip to content

Instantly share code, notes, and snippets.

@pradeepn
Created March 8, 2017 10:47
Show Gist options
  • Save pradeepn/47b6c6aba4fbc88c798c41b758c20590 to your computer and use it in GitHub Desktop.
Save pradeepn/47b6c6aba4fbc88c798c41b758c20590 to your computer and use it in GitHub Desktop.
Angular - Manual Bootstrapping
<script>
angular.module('config', []);
angular.module('myApp', ['config']);
window.onload = function(){
// get injector object
var initInjector = angular.injector(['ng']);
// extract necessary angular services
var $http = initInjector.get('$http');
var $timeout = initInjector.get('$timeout');
var $animate = initInjector.get('$animate');
// do operations before bootstrap
// get user sign in status
$http({
url : 'https://api.mysite.com/auth/user/basic',
method : 'GET',
headers : {
'x-auth-token' : localStorage.getItem('auth-token')
}
})
// when first request resolves
// store user sign in status and other info
// as a constant in `config` module
.then(
// signed in {statusCode : 200} // OK
function(res){
angular.module('config').constant('__user', {
$state : 'signed',
$accType : res.data.accountType,
$accData : res.data
});
},
// not signed in {statusCode : 403} // Forbidden
function(){
angular.module('config').constant('__user', {
$state : 'unsigned'
});
}
)
// resolves on either success or failed response
// of previous authentication request
.then(function(){
// start bootstrapping
angular.bootstrap(document, ['myApp']);
// add `_splash_fade_out` class to splash screen
// when resolved after animation complete, remove element from DOM
$animate
.addClass(angular.element('splash-screen'), '_splash_fade_out')
.then(function(){
angular.element('splash-screen').remove();
});
});
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment