Learn AngularJS Modules: Part 7

Before diving into modules the following links are recommended:

AngularJS Modules

We have various parts in our applications, like controllers, directives, services, filters and so on. These parts are contained within a container called Modules. All the application controllers should belong to the module to define our application.
       
Advantages

What most of the web applications actually runs is a main method that is responsible for instantiation and the mapping of the various parts of the application. As we know, there is no such main method in AngularJS, in this scenario the concept of Modules comes into play where it declaratively specifies the roadmap of application bootstapping.

Angularjs also solves the issues of global value, such as global varialbles or global functions in which they can easily be destroyed by other scripts.

Besides this, there are the following additional advantages of using modules in applications.

  1. the application becomes more readable.
  2. It makes the application unit testing process faster since it loads relevant modules.
  3. Reusuability of code can be done.
  4. Easier to understand.
  5. Keeps a clean global namespace.

Creating AngularJS

  1. var module1 = angular.module('module1', []); //creating a module  
Use
  1. <!DOCTYPE html>  
  2. <html ng-app="module1" xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>  
  6. </head>  
  7. <body>  
  8.     <script>  
  9.         // Angularjs script here   
  10.     </script>  
  11.     
  12. </body>  
  13. </html>  
Without Module
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Module</title>  
  5.     <script src="angular.min.js"></script>  
  6.       
  7. </head>  
  8. <body>  
  9.     <div ng-app="" ng-controller="detailscontroller">  
  10.         {{ Name + " " + dept }}  
  11.     </div>  
  12.     <script>  
  13.         function detailscontroller($scope) {  
  14.             $scope.Name = "iShriss";  
  15.             $scope.dept = "IT";  
  16.         }  
  17.     </script>  
  18. </body>  
  19. </html>  
This code will give the output iShriss IT.

Using Module
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head><title>using module</title>  
  5.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>  
  6. </head>  
  7. <body>  
  8.     <div ng-app="module1" ng-controller="detailscontroller">  
  9.         {{ Name + " " + dept }}  
  10.     </div>  
  11.     <script>  
  12.         var module1 = angular.module("module1", []);  
  13.         module1.controller("detailscontroller"function ($scope) {  
  14.             $scope.Name = "iShriss";  
  15.             $scope.dept = "IT";  
  16.         });  
  17.     </script>  
  18. </body>  
  19. </html>  
After learning about modules and how they work let's build our application file consisting of at least on module file and a controller file for each controller.

Let's create a module.
  1. var module1 = angular.module("module1", []);  
Creation and Retrieval of Module

In the application angular.module('module1 '.[]) will actually create the module module1 and also override the existing module with the same name whereas angular.module('module1') retrieves the existing module module1.
  1. var module1 = angular.module('module1', []); //creating a module  
  2.   
  3. // adding some directives and services  
  4. module1.service('Service1', ...);  
  5. module1.directive('Directive1', ...);  
  6.   
  7. // overwrites both myService and myDirective by creating a new module  
  8. var module1 = angular.module('module1', []);  
Note

We can define dependent modules using [] parameter in the module definition. After building the module, let's create a controller file.
  1. module1.controller("detailscontroller"function ($scope) {  
  2.     $scope.Name = "iShriss";  
  3.     $scope.dept = "IT";  
Summary

So far so good ? Yes. We at least know what AngularJS Modules are, how they work and how they are used in applications. We also touched on the advantanges of using modules in applications, if the motive of this article is worthwhile.

Happy Coding folks!

Up Next
    Ebook Download
    View all
    Learn
    View all