Site icon Tutorial

AngularJS Bootstrap

We explain the AngularJS initialization process and how you can manually initialize AngularJS if necessary.

AngularJS <script> Tag

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

<!doctype html>

<html xmlns:ng=”http://angularjs.org” ng-app>

<body>

<script src=”angular.js”></script>

</body>

</html>

Automatic Initialization

AngularJS 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 AngularJS looks for the ngApp directive which designates your application root. If the ngApp directive is found then AngularJS will:

<!doctype html>

<html ng-app=”optionalModuleName”>

<body>

I can add: {{ 1+2 }}.

<script src=”angular.js”></script>

</body>

</html>

As a best practice, consider adding an ng-strict-di directive on the same element as ng-app:

<!doctype html>

<html ng-app=”optionalModuleName” ng-strict-di>

<body>

I can add: {{ 1+2 }}.

<script src=”angular.js”></script>

</body>

</html>

This will ensure that all services in your application are properly annotated.

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 AngularJS compiles a page. Here is an example of manually initializing AngularJS:

<!doctype html>

<html>

<body>

<div ng-controller=”MyController”>

Hello {{greetMe}}!

</div>

<script src=”http://code.angularjs.org/snapshot/angular.js”></script>

<script>

angular.module(‘myApp’, [])

.controller(‘MyController’, [‘$scope’, function ($scope) {

$scope.greetMe = ‘World’;

}]);

angular.element(function() {

angular.bootstrap(document, [‘myApp’]);

});

</script>

</body>

</html>

Note that we provided the name of our application module to be loaded into the injector as the second parameter of the angular.bootstrap function. Notice that angular.bootstrap will not create modules on the fly. You must create any custom modules before you pass them as a parameter.

You should call angular.bootstrap() after you’ve loaded or defined your modules. You cannot add controllers, services, directives, etc after an application bootstraps.

This is the sequence that your code should follow:

There are a few things to keep in mind regardless of automatic or manual bootstrapping:

Deferred Bootstrap

This feature enables tools like Batarang and test runners to hook into angular’s bootstrap process and sneak in more modules into the DI registry which can replace or augment DI services for the purpose of instrumentation or mocking out heavy dependencies.

If window.name contains prefix NG_DEFER_BOOTSTRAP! when angular.bootstrap is called, the bootstrap process will be paused until angular.resumeBootstrap() is called.

angular.resumeBootstrap() takes an optional array of modules that should be added to the original list of modules that the app was about to be bootstrapped with.

Exit mobile version