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.

Note:
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 https://code.angularjs.org/


<script src=”https://code.angularjs.org/1.3.0/angular.js” type=”text/javascript”>

AngularJs bootstrap process happens on document ready event.


lets see the simple AngularJs example.


<html>
 <head>
    <title>Angular Application</title>
 </head>
 <body ng-app="myApp">
    <div ng-controller="myCtrl">
        <h3>{{title}}</h3>
    </div>

    <script src="https://code.angularjs.org/1.3.0/angular.js" type="text/javascript"></script>

    <script type="text/javascript">

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

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

    </script>

 </body>
</html>


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.


Comments

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

How to create SSH Key for GitHub and Bitbucket

This  post  demonstrate how to  generate  SSH private and  public   key  with fundamental steps and  assist   you  to  add   public   key  inside your  GitHub  and Bitbucket account. But first why we  need  this SSH  public   key ? Whether you are working in the team or an individual and you want make any operations on secure GIT repository like clone or push your latest changes then you require to provide your credentials to help GIT to authenticate you and approve those operations. But when  you  do so,  you  are  asked  to  provide  your username &  password ,  each   time  on  each   operation . It looks  simple  earlier, but later  becomes  a  headache . SSH  connection   helps   us  to  authenticate  using public and private keys, that  we  generate only once, later  we  don’t need to  authenticate ,  SSH  will  do  this behalf of  us . But how does it  work ? SSH   authenticate  you  using  an  identity , your  identity  and this  identity   comprise   combination  of p