Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding AngularJS Bootstrap Process

  Author : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 8,309   
 

Angular initializes automatically upon DOMContentLoaded event or when the angular.js script is downloaded to the browser and the document.readyState is set to complete. At this point AngularJS looks for the ng-app directive which is the root of angular app compilation and tells about AngularJS part within DOM. When the ng-app directive is found then Angular will:

  1. Load the module associated with the directive.

  2. Create the application injector.

  3. Compile the DOM starting from the ng-app root element.

This process is called auto-bootstrapping.

<html>
<body ng-app="myApp">
 <div ng-controller="Ctrl">
 Hello {{msg}}!
 </div>

 <script src="lib/angular.js"></script>
 <script>
 var app = angular.module('myApp', []);
 app.controller('Ctrl', function ($scope) {
 $scope.msg = 'World';
 });
 </script>
</body>
</html>

Manual Bootstrap Process

You can manually initialized your angular app by using angular.bootstrap() function. This function takes the modules as parameters and should be called within angular.element(document).ready() function. The angular.element(document).ready() function is fired when the DOM is ready for manipulation.

<html>
<body>
 <div ng-controller="Ctrl">
 Hello {{msg}}!
 </div>
 <script src="lib/angular.js"></script>

 <script>
 var app = angular.module('myApp', []);
 app.controller('Ctrl', function ($scope) {
 $scope.msg = 'World';
 });

 //manual bootstrap process
 angular.element(document).ready(function () {
 angular.bootstrap(document, ['myApp']);
 });
 </script>
</body>
</html>

Note

  1. You should not use the ng-app directive when manually bootstrapping your app.

  2. You should not mix up the automatic and manual way of bootstrapping your app.

  3. Define modules, controller, services etc. before manually bootstrapping your app as defined in above example.

What do you think?

I hope you will enjoy the AngularJS bootstrap process while developing your app with AngularJS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
11 DEC
ASP.NET MVC with AngularJS Development (offline)

Sat-Sun 09:30 AM-11:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (11:00 AM-12:30 PM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+