Please enable Javascript to correctly display the contents on Dot Net Tricks!

Bootstrapping angular app based on multiple modules

  Author : Shailendra Chauhan
Total Views : 7,887   
 

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:

  1. 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 = "Shailendra Chauhan";
     });
    
     //module2
     var app2 = angular.module("module2", []);
     app2.controller("Controller2", function ($scope) {
     $scope.name = "Deepak Chauhan";
     });
    
     //module3 dependent on module1 & module2
     angular.module("app", ["module1", "module2"]);
     </script>
    </head>
    <body>
     <!--angularjs autobootstap 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>
    
    
  2. 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 = "Shailendra Chauhan";
     });
    
     //module2
     var app2 = angular.module("module2", []);
     app2.controller("Controller2", function ($scope) {
     $scope.name = "Deepak Chauhan";
     });
    
     //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 autobootstap 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.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+