Skip to main content

AngularJs Tutorials : Bootstrap

Every application starts with bootstrap process which initialize application and wire it other with dependencies and configurations.

AngularJs is not different from other application. It also starts application with bootstrap process.

Following operation happens inside AngularJs bootstrap process.

  1. Load application module.
  2. Create dependency injector and load dependencies.
  3. Compile HTML and create scope for application.

All these steps happens inside angular.js scripting file. therefore we need to include it first.
we can include it inside HEAD tag or at end of BODY tag.

Adding angular.js file at end of body tag will allow browser to load of HTML elements without any delay and afterwards load angular.js and begin bootstrapping process.

You can get angular.js source file from

<script src=”” type=”text/javascript”>

AngularJs bootstrap process happens on document ready event.

lets see the simple AngularJs example.

    <title>Angular Application</title>
 <body ng-app="myApp">
    <div ng-controller="myCtrl">

    <script src="" type="text/javascript"></script>

    <script type="text/javascript">

        //angular module

        .controller("myCtrl", ["$scope", function($scope){
            $scope.title = "Angular Bootstrap Tutorial!"



Now lets look into AngularJs bootstrap process steps one by one.

  1. Load application module:

    AngularJs will look for module name which is associated with ng-app directive inside HTML page.

    <html ng-app="myApp" >

    Then Angular will load this module from JavaScript snippet.

    angular.module("myApp", []) 

  1. Create dependency injector and load dependencies

    After first step AngularJs will create injector and compiler objects. functionality of compiler object is listed in third step.
    Here injector object will look for dependency of application module. In above example we have not specify any dependency.
    Following example illustrate module dependency

    angular.module("myApp",[ "ngResource"])

    These dependencies are nothing but angular modules. injector load this modules and allow them to be used inside angular application.

    Services can also be injected on fly inside application controller or application services.

    .controller("myCtrl", ["$scope", function($scope){
      $scope.title = "Angular Bootstrap Tutorial!"

    in above example $scope is injected by injector inside controller and we can use it inside controller.
  2. Compile HTML and create scope for application.

    Inside last step AngularJs compiles HTML elements using compiler object then creates scope for application and bind rootScope to html element where we have specified ng-app.
    In above example ng-app is specified inside body tag which means rootScope is attached to body tag and all template logic get applied on this body tag.

Above scenario illustrates automatic bootstrapping process. but some time you may require to manually bootstrap AngularJs application where you need to perform some custom JavaScript task before initiating AngularJs application.

Following script snippet shows manual bootstrapping process.

// document ready event
angular.element(document).ready( function() {

    // bootstrap document with myApp module
    angular.bootstrap(document, ["myApp"])


As stated earlier bootstrap happens on document ready event therefore you need to configure it first.
Then call angular.bootstrap function which will accept two parameters.

First parameter is html element e.g. document, html, body or particular div. this process is equivalent as specifying ng-app directive to HTML element.

Second parameter is AngularJs module name. in this case we have give myApp.

Note that you first need to add module scripting code then initiate bootstrap process otherwise bootstrap will throw error that unable to load given module.

With automatic or manual bootstrap, you are ready to create beautiful AngularJs application.


Popular posts from this blog

AngularJs Tutorial: Getting Started

This tutorial will help you to create a dynamic web application using power of AngularJs. ·   What is AngularJs AngularJs is javascript MVC framework (maintained by Google) which molds static HTML application to dynamic web application. That was a brief introduction, you can find more information on Wiki and AngularJS Site . AngularJs follows MVC pattern to separate out model from view and manage it by controller. See this image Goal of using AngularJs is to convert static HTML template to dynamic (Angular) template.   ·   How to convert static template to dynamic (Angular) Template Now consider the following static HTML template which contain list of movies title. <html> <head></head> <body>   <h1>Upcoming Movies</h1> <ul>     <li>       <span>Godzilla</span>     </li> <li>       <span>Batman vs Superman</span>     </li> <li>       <span>Sta

Abstract Factory design pattern analysis

What is Abstract Factory design pattern?     The intent or definition of Abstract Factory is “ Provide an interface for creating families of related or dependent objects without specifying their concrete classes ” It is kind of an obscure definition, in order to understand it we need to break it into meaningful pieces. 1.  " Provide an interface "  When we talk about Abstract Factory we talk about interface not abstract class , Basically this design pattern gives us an Interface, like Interface in Java, C# , Typescript (abstract class).    Why interface?  Whenever we talk about any design pattern, it emphasizes more on how we are going to use it rather than implement it. Interface is always the right type to prefer because concrete class creates tight coupling whereas an interface isolates it from the rest of the system. At runtime we can get different solution, without updating our code. For example interface SecurityFactory