Improve this doc

Overview

This page explains the Angular initialization process and how you can manually initialize Angular if necessary.

Angular <script> Tag

This example shows the recommended path for integrating Angular with what we call automatic initialization.

<!doctype html>
<html xmlns:ng="http://angularjs.org" ng-app>
  <body>
    ...
    <script src="angular.js">
  </body>
</html>

Automatic Initialization

Angular initializes automatically upon DOMContentLoaded event or when the angular.js script is evaluated if at that time document.readyState is set to 'complete'. At this point Angular looks for the ng-app directive which designates your application root. If the ng-app directive is found then Angular will:

<!doctype html>
<html ng-app="optionalModuleName">
  <body>
    I can add: {{ 1+2 }}.
    <script src="angular.js"></script>
  </body>
</html>

Manual Initialization

If you need to have more control over the initialization process, you can use a manual bootstrapping method instead. 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.

Here is an example of manually initializing Angular:

<!doctype html>
<html xmlns:ng="http://angularjs.org">
  <body>
    Hello {{'World'}}!
    <script src="http://code.angularjs.org/angular.js"></script>
    <script>
       angular.element(document).ready(function() {
         angular.bootstrap(document, ['optionalModuleName']);
       });
    </script>
  </body>
</html>

Note that we have provided the name of our application module to be loaded into the injector as the second parameter of the api/angular.bootstrap function. This example is equivalent to using the ng-app directive, with ng-app="optionalModuleName", as in the automatic initialization example above.

This is the sequence that your code should follow:

  1. After the page and all of the code is loaded, find the root element of your AngularJS application, which is typically the root of the document.

  2. Call api/angular.bootstrap to compile the element into an executable, bi-directionally bound application.