• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • AngularJs

Bootstrapping multiple modules in AngularJS

  • Arif Khoja
  • April 26, 2018
  • 3 minute read
AngularJs LOGO
Total
0
Shares
0
0
0

In this article we will be Understanding AngularJS Bootstrap Process talking about automatic Bootstrapping and manual Bootstrapping multiple modules in AngularJS.

Angular initiates automatically upon [marker color=”#dbdbdb” textcolor=”#1e73be”]DOMContentLoaded[/marker] event or when the angular.js script is downloaded to the browser and the [marker color=”#dbdbdb” textcolor=”#1e73be”]document.readyState[/marker] is set to [marker color=”#dbdbdb” textcolor=”#1e73be”]complete[/marker].

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.

module

Example

<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> 

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.

Example

<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.

AngularJS is automatically initialized for one module. But sometimes, it is required to bootstrap for multiple modules and it can be achieved by using two methods:

Automatic bootstrap (by combining multiple modules into one module)

You can combine multiple modules into single modules and your angular app will be automatically initialized for newly created module and other modules will act as dependent modules for newly created module.

For example, suppose you have two modules: module1 and model2, and you have to initialize your app automatically based on these two modules then you achieve this following way:

<html>
<head>
    <title>Multiple modules bootstrap</title>
    <script src="lib/angular.js"></script>
    <script>
        //module1
        var app1 = angular.module("module1", []);
        app1.controller("Controller1", function ($scope) {
            $scope.name = "Arif Khoja";
        });
        //module2
        var app2 = angular.module("module2", []);
        app2.controller("Controller2", function ($scope) {
            $scope.name = "Yatendra Joddha";
        });
        //module3 dependent on module1 & module2
        angular.module("app", ["module1", "module2"]);
    </script>
</head>
<body>
    <!--angularjs auto bootstrap process-->
    <div ng-app="app">
        <h1>Multiple modules bootstrap</h1>
        <div ng-controller="Controller2">
            {{name}}
        </div>
        <div ng-controller="Controller1">
            {{name}}
        </div>
    </div>
</body>
</html>

Manual bootstrap

 You can manually bootstrap your app by using angular.bootstrap() function, for multiple modules. The above example can be rewritten as for manual bootstrap process as given below:

<html>
<head>
    <title>Multiple modules bootstrap</title>
    <script src="lib/angular.js"></script>
    <script>
        //module1
        var app1 = angular.module("module1", []);
        app1.controller("Controller1", function ($scope) {
            $scope.name = "Arif Khoja";
        });
        //module2
        var app2 = angular.module("module2", []);
        app2.controller("Controller2", function ($scope) {
            $scope.name = "Yatendra Joddha";
        });
        //manual bootstrap process
        angular.element(document).ready(function () {
            var div1 = document.getElementById('div1');
            var div2 = document.getElementById('div2');
            //bootstrap div1 for module1 and module2
            angular.bootstrap(div1, ['module1', 'module2']);
            //bootstrap div2 only for module1
            angular.bootstrap(div2, ['module1']);
        });
    </script>
</head>
<body>
    <!--angularjs manual bootstrap process-->
    <div id="div1">
        <h1>Multiple modules bootstrap</h1>
        <div ng-controller="Controller1">
            {{name}}
        </div>
        <div ng-controller="Controller2">
            {{name}}
        </div>
    </div>
    <div id="div2">
        <div ng-controller="Controller1">
            {{name}}
        </div>
    </div>
</body>
</html>

 

What do you think?

I hope you have got, how to bootstrap your angular app based on multiple modules. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Arif Khoja
Arif Khoja

Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.

Views: 6,794

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

 

Loading Comments...
 

    %d