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

Subscribe for latest tutorial updates

* indicates required

Popular posts from this blog

Preload Images Using Javascript

Preload Image is technique which helps browser to render images with minimum delay. Today we will see example of image gallery. Gallery contain 5 images which load images one after another whenever user clicks next and back button. This is a basic image gallery scenario which is used in all possible website, then why we need preloaded images for this gallery? Now a day’s most of website becoming faster and user expectation is increasing. Suppose your website doesn’t use preload image technique for gallery and a user visits any image gallery from Google Plus or Facebook and visits your website gallery. Then that user always prefer those websites rather than yours. Why? Because your website load one image at a time. When user click on next button, then only gallery load image and user has wait till it get loaded. To avoid this situation gallery needs to download all images

Spring Boot RestTemplate Guide with Example

What is RestTemplet? RestTemplate is a HTTP Client library that provides basic APIs for executing different HTTP type requests. RestTemplate is one of the HTTP libraries available on the market. The Spring Boot team designed and maintains it. As previously said, it is a client library, which means it may be utilised in our application as a stand-alone dependency. To utilise it, you don't require a complete Spring boot application. It's suitable for use in simple Java applications as well as other frameworks such as Micronaut, Play and others. Setup Add RestTemplate dependency to project, for this tutorial I am creating a simple java application to consume HTTP request and a node application to provide HTTP service. For Gradle build tool Add the following dependency in the build.gradle file . // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web implementation group: 'org.springframework.boot' , name: 'spring-boot-starter-w

CSS: How To Create Custom Scrollbar For Webkit Supported Browsers

In this tutorial, we will learn how to create a custom scroll bar using custom CSS styles. Custom scrollbars are becoming increasingly popular, and I'm excited to learn more about them. A scrollbar can be customised for a variety of reasons. The default scrollbar, for example, can make an app's UI look inconsistent across various versions of windows, thus we can benefit from having a single style here. This tutorial will help to create a custom scrollbar. Let's see how One of the most interesting aspects of these scrollbars is that they may be rendered in a variety of styles for different HTML elements on the same webpage. We will see the implementation of such a scrollbar for Webkit-supported browsers. First we will look at the basic CSS properties of the scrollbar. ::-webkit-scrollbar              ::-webkit-scrollbar-thumb        ::-webkit-scrollbar-track        ::-webkit-scrollbar-button       ::-webkit-scrollbar-track-piece  ::-webkit-scrollbar-cor