I would like to manually bootstrap my angular app on dom load but Angular doesn't seem to be able to bootstrap my app when i try to wait until dom load
<body>
<div ng-app="myApp">
{{1 + 1}}
</div>
<script src="angular.js"></script>
<script type="text/javascript" src="delay.js"></script>
</body>
And here's delay.js
window.onload = function(){
console.log('reached 2')
var myApp = angular.module('myApp',[])
}
console.log('reached 1')
The above code throws with the following error.
reached 1
Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify th...<omitted>...0)
reached 2
After looking at various other examples, the bootstrapping seems to work seamlessly when i do it outside the window.onload event like this.
delay.js
//this bootstrap works
angular.module('myApp',[])
console.log('reached 1')
Not sure what's happening here, is the module not visible to angular during the onload callback ?
Further even the following in delay.js fails
//still fails to bootstrap
angular.element(document).ready(function(){
console.log('reached 2')
var myApp = angular.module('myApp',[])
})
Even the following fails to work
angular.element(document).ready(function(){
console.log('reached 2')
angular.bootstrap(document,[myApp])
var myApp = angular.module('myApp',[])
})
OR
angular.element(document).ready(function(){
console.log('reached 2')
var myApp = angular.module('myApp',[])
angular.bootstrap(document,[myApp])
})
OR even
angular.element(document).ready(function(){
angular.bootstrap(document.body.children[0],['myApp'])
var myApp = angular.module('myApp',[])
console.log('reached 2')
})