Use Multiple Modules On Same Page In AngularJS


In this article I'll explain 'How to Use Multiple Modules on the Same Page.'

While you are working with AngularJS, you might find a situation where you are having some scopes which are available in some controller, but those controllers belong to different modules; in such a case you might have introduced new scope in controller so that you can use it. This is because you can't declare multiple modules in "ng-app", this is a limitation of ng-app, it restricts you to only one module per page. To get rid of this problem you can choose "ng-module" which is available in "". is a new directive which needs to be provided where you had provided the AngularJS directive, you can download it from my source code.

I am creating a sample application to show how you can use this feature.

Step 1: Firstly, download the "" using the above links.

After this provide where you had provided the "angularjs" directive.

  1. <head>  
  2.     <title></title>  
  3.     <script src="angular.js"></script>  
  4.     <script src=""></script>  
  5.     <script src="AngularController.js"></script>  
  6. </head>  

Step 2: Now I am creating a new JavaScript file where we will declare our modules, controller, and scopes.

  1. var firstModule = angular.module('firstModule', []);  
  3. firstModule.controller('firstController'function ($scope) {  
  4.     $scope.UserName = 'Anubhav Chaudhary';  
  5. });  
  7. firstModule.controller('secondController'function ($scope) {  
  8.     $scope.MobileNumber = '00000000';  
  9. });  
  11. var secondModule = angular.module('secondModule', []);  
  13. secondModule.controller('thirdController'function ($scope) {  
  14.     $scope.EmailId = '';  
  15. });  

Here you can see that I declared two modules named "firstModule" and "secondModule," inside the firstModule I created two controllers and in those controllers I created some scope properties, inside secondModule only one controller is created and in the controller one scope property is created. To all these scopes some default value is also provided.

Step 3: Now it's time to work on the main section i.e. HTML section.

  1. <!DOCTYPE html>  
  2. <html xmlns="">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="angular.js"></script>  
  6.     <script src=""></script>  
  7.     <script src="AngularController.js"></script>  
  8. </head>  
  9. <body>  
  10.     <form ng-modules="firstModule,secondModule">  
  11.         <fieldset>  
  12.             <legend>We are having both the modules</legend>  
  13.             <div ng-controller="firstController">  
  14.                 Provide Your Name :  
  15.             <input type="text" ng-model="UserName" />  
  16.             </div>  
  17.             <div ng-controller="secondController">  
  18.                 Provide Your Mobile Number :  
  19.             <input type="text" ng-model="MobileNumber" />  
  20.             </div>  
  21.             <div ng-controller="thirdController">  
  22.                 Provide Your Email ID :  
  23.             <input type="text" ng-model="EmailId" />  
  24.             </div>  
  25.         </fieldset>  
  26.     </form>  
  27.     <form ng-module="secondModule">  
  28.         <fieldset>  
  29.             <legend>I am having only second module</legend>  
  30.             <div ng-controller="thirdController">  
  31.                 Provide Your Email ID :  
  32.             <input type="text" ng-model="EmailId" />  
  33.             </div>  
  34.         </fieldset>  
  35.     </form>  
  36. </body>  
  37. </html>  

Here you can see that I created two forms, let's firstly talk about the first form.

In the first form you can see that I had provided both the modules using "ng-modules" directive. When you are using "ng-modules" then it allows you to use multiple modules at same time, but if you don't wan't to use multiple modules then you can use "ng-module" directive.

Inside the first form all three controllers are used in three different div's and their scopes are bind to some input fields.

In the second form you can see that I have used "ng-module" because I need to use only one module here. Inside the form div is bound to a corresponding controller and an input element is bound to scope property.

Now our application is created and it's time to see the output.

Output: On running the application you will see an output like this,

All the input fields have their scope value and this means that our code has run successfully and both the modules got bound at same time.