Sunday, July 30, 2017

Module in Angular

  • A module is a container for component of angular - controllers, directives, services, filters etc.
  • It supports separation of concern using modules.
  • It is an entry point. Using module we can decide how the AngularJS application should be bootstrapped.

We can create a simple module using the following code.
 Example: 
           var myAppModule = angular.module('myApp', []);

In the above example, the angular.module() method creates an application module, where 
  1. First parameter is a module name which is same as specified by ng-app directive.
  2. Second parameter is an array of other dependent modules [ ]. 
In the above example we are passing an empty array because there is no dependency.

Type of Bootstrapping
1.Automatic bootstrapping – In tag of html page
   Example: 
          <html ng-app=“module Name”>

2.Manual bootstrapping– In JS by fetching DOM element.
Example: 
                          var app = angular.module('myApp', [])

                                                   .controller('NameController', function ($scope) {
                                                                     $scope.StdName = 'Amit';
                                                            });
                           //manual bootstraping 
                           angular.element(document).ready(function () {
                                          angular.bootstrap(
                                             document.getElementsByTagName(‘html’),[‘myApp’]);
                                  });
Angular initializes automatically when DOM content is completely loaded or when the angular.js script is evaluated.

Why Manual Bootstrap
  • Asynchronously loaded data that need to perform an operation before Angular compiles a page.
  • To create App build with Cordova. Cordova is a platform that is used for building mobile apps using HTML, CSS and JS.
IMPORTANT: 
  • Not mix up the automatic and manual bootstrapping your app.
  • No need use the ng-app directive when manually bootstrapping your app.
  • Define the modules, controller etc. before manually bootstrapping .

No comments:

Post a Comment