Angularjs manual bootstrap -


i manually bootstrap angular app on dom load angular doesn't seem able bootstrap app when try 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 following error.

reached 1  uncaught error: [$injector:modulerr] failed instantiate module myapp due to: error: [$injector:nomod] module 'myapp' not available! either misspelled module name or forgot load it. if registering module ensure specify th...<omitted>...0)  reached 2  

after looking @ various other examples, bootstrapping seems work seamlessly when outside window.onload event this.

delay.js

//this bootstrap works  angular.module('myapp',[]) console.log('reached 1') 

not sure what's happening here, module not visible angular during onload callback ?

further following in delay.js fails

//still fails bootstrap      angular.element(document).ready(function(){     console.log('reached 2')     var myapp = angular.module('myapp',[])     }) 

even following fails 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

angular.element(document).ready(function(){ angular.bootstrap(document.body.children[0],['myapp']) var myapp = angular.module('myapp',[]) console.log('reached 2') }) 

angularjs bootstraps automatically on domcontentloaded. event fires dom ready. means don't have manually bootstrap app if need dom being ready.

the window.onload event waits content loaded. includes scripts, images , other resources.

in case still want manually bootstrap on ready, how it:

angular.element(document).ready(function() {     angular.bootstrap(document, ['myapp']); }); 

angular.bootstrap create app on element specified. in case on document. means don't need have ng-app directive if manually bootstrap app. (please note first incorrectly assumed still need it.)

you can not have module declaration inside ready callback, because stated in developer guide:

"notice angular.bootstrap not create modules on fly. must create custom modules before pass them parameter."

here corrected fiddle shows ng-app isn't needed when manually bootstrapping: http://jsfiddle.net/yxaba/


Comments

  1. This is so true, and In order to make use of the features of AngularJS, you need to hire an angular JS developers or a team of developers who have expertise in using this framework. Well, one can hire a professional freelancer from Eiliana.com, which is a global freelancing platform, and all the freelancers are highly skilled.

    ReplyDelete

Post a Comment

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

ios - Can NSManagedObject conform to NSCoding -

What is the difference between data design and data model(ERD) -