2

I have seen two different ways to start up AngularJS:

  • ng-app="TodoApp"
  • angular.bootstrap(angular.element("body")[0], ["TodoApp"]);

First can someone tell me if these are both the same?

I also see the following code:

var TodoApp = angular.module('TodoApp', ['ngResource']);

Does the code above have to have been loaded before I can issue the angular.bootstrap command?

2 Answers 2

4

ng:app

Directive* ng:app can be used to auto-bootstrap the application. One can place multiple ng-app directives inside HTML but only the first one will be used to bootstrap the app, while others will be ignored. The element at which the first ng:app directive is found becomes the 'root' of the AngularJS application (this is where the $rootScope gets attached).

[using ng:app] Angular initializes automatically upon DOMContentLoaded event ...

*Although ng:app is used similarly to other directives, it's not part of the ng module and it is not defined the same way other Angular directives are defined.  

angular.bootstrap

On the other hand, with manual bootstrapping you may initialise multiple distinct Angular apps inside the same page.

Examples of when you'd need to do this include using script loaders or the need to perform an operation before Angular compiles a page.  

So if you want to use a script loader such as RequireJS, than, in RequireJS runner method, you will want to initialise your angular app by calling angular.bootstrap(....

Angular.bootstrap command lets you specify the module dependencies as a second parameter. The signature of the function is:

angular.bootstrap(element[, modules]);

When you want to bootstrap the app manually than you have to specify the name of your main angular module inside the dependency array (the second parameter to angular.bootstrap call):

// You define your app module:
angular.module('myApp', []);
// Then you specify your app module as a dependency to angular.bootstrap:
angular.bootstrap(element, ['myApp']);

  Also, manual bootstrapping lets you do some pre-processing and configuring outside the angular before initialising your angular app.  

Load order

To answer the second part of your question: Yes, you need to load and execute your modules before you can reference them in call to angular.bootstrap.

angular.module('TodoApp', ['ngResource']);
angular.bootstrap(document, ['TodoApp']);
Sign up to request clarification or add additional context in comments.

Comments

1
  1. Those both lines are the same (assuming you have ng-app on your body element).

  2. You sould do in this order:

    1. include angular.js
    2. include all your code (controllers, etc)
    3. call bootstrap

Note, after calling bootstrap you can't define new modules (at least for now, as there is no dynamic module loading and lazy loading). This is because there is several steps of loading AngularJS application - configure and run. And after run there is not possible to run configure steps of other modules.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.