AngularJS | Modules - GeeksforGeeks

31 May, 2019
The AngularJS module defines the functionality of the application which is applied on the entire HTML page. It helps to link many components. So it is just a group of related components. It is a container which consists of different parts like controllers, services, directives.
Note: This modules should be made in a normal HTML files like index.html and no need to create a new project in VisualStudio for this section.
How to create a Module:
var app = angular.module("Module-name", []);
In this [] we can add a list of components needed but we are not including any components in this case. This created module is bound with any tag like div, body, etc by adding it to the list of modules.
<div ng-app = "module-name">    The code in which the module is required.</div>
Adding a Controller:
app.controller("Controller-name", function($scope) {
    $scope.variable-name= "";
});
Here, we can add any number of variables in controller and use them in the html files, body of the tag in which the controller is added to that tag by writing:
<body><div ng-app="Module-name">    <div ng-controller="Controller-name">        {{variable-name}}    </div>  <!-- This wont get printed since itsnot part of the div in which controller is included -->{{variable-name}}  </div></body>
Module and Controllers in Files: While we can make modules and controllers in the same file along with the HTML file which requiring it however we may want to use this module in some other file. Hence this will lead to redundancy so we will prefer to create Module, Controller and HTML file separately. The Module and Controller are to be stored by using .js files and in order to use them in the HHTML file we have to include them in this way:
Example:
DemoComponent.js// Here the Component name is DemoComponent// so saving the file as DemoComponent.js app.controller('DemoController', function($scope) {        $scope.list = ['A', 'E', 'I', 'O', 'U'];    $scope.choice = 'Your choice is: GeeksforGeeks';          $scope.ch = function(choice) {        $scope.choice = "Your choice is: " + choice;    };          $scope.c = function() {        $scope.choice = "Your choice is: " + $scope.mychoice;    };});
// Here the Component name is DemoComponent// so saving the file as DemoComponent.js app.controller('DemoController', function($scope) {        $scope.list = ['A', 'E', 'I', 'O', 'U'];    $scope.choice = 'Your choice is: GeeksforGeeks';          $scope.ch = function(choice) {        $scope.choice = "Your choice is: " + choice;    };          $scope.c = function() {        $scope.choice = "Your choice is: " + $scope.mychoice;    };});
Module-name: DemoApp.jsvar app = angular.module('DemoApp', []);
var app = angular.module('DemoApp', []);
index.html file<!DOCTYPE html><html>  <head>    <title>        Modules and Controllers in Files    </title>    </head>  <body ng-app="DemoApp">    <h1>        Using controllers in Module    </h1>          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">    </script>          <script src="DemoApp.js"></script>    <script src="DemoController"></script>           <div ng-app="DemoApp" ng-controller="DemoController">        Vowels List : <button ng-click="ch('A')" >A</button>                   <button ng-click="ch('E')" >E</button>        <button ng-click="ch('I')" >I</button>        <button ng-click="ch('O')" >O</button>        <button ng-click="ch('U')" >U</button>                  <p>{{ choice }}</p>               Vowels List :        <select  ng-options="option for option in list"             ng-model="mychoice" ng-change="c()">        </select>                  <p>{{ choice }}</p>    </div></body>  </html>
<!DOCTYPE html><html>  <head>    <title>        Modules and Controllers in Files    </title>    </head>  <body ng-app="DemoApp">    <h1>        Using controllers in Module    </h1>          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">    </script>          <script src="DemoApp.js"></script>    <script src="DemoController"></script>           <div ng-app="DemoApp" ng-controller="DemoController">        Vowels List : <button ng-click="ch('A')" >A</button>                   <button ng-click="ch('E')" >E</button>        <button ng-click="ch('I')" >I</button>        <button ng-click="ch('O')" >O</button>        <button ng-click="ch('U')" >U</button>                  <p>{{ choice }}</p>               Vowels List :        <select  ng-options="option for option in list"             ng-model="mychoice" ng-change="c()">        </select>                  <p>{{ choice }}</p>    </div></body>  </html>
Output:
Note: It makes sure the module and component files are in the same folder otherwise provide the path in which they are saved and run.
Directives in a Module: To add a directive in module follow the steps:
Creating a module like we did earlier:var app = angular.module("DemoApp", []);
var app = angular.module("DemoApp", []);
Creating a directive:app.directive("Directive-name", function() {
    return {
        template : "string or some code which is to be executed"
    };
});

app.directive("Directive-name", function() {
    return {
        template : "string or some code which is to be executed"
    };
});

Example:
<!DOCTYPE html><html>   <head>    <title>        Modules and Controllers in Files    </title>          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">    </script></head>       <body>    <div ng-app="GFG" w3-test-directive></div>    <script>  var gfg_app = angular.module("GFG", []);  gfg_app.directive("w3TestDirective", function() {      return {          template : "Welcome to GeeksforGeeks!"      };  });  </script>  </body></html>
Output:
Welcome to GeeksforGeeks!
Note: Here anything to be printed should not be put in the div which is calling the directive since it gets overwritten by the code in the template.
Picked
AngularJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 10 Angular Libraries For Web Developers
Angular File Upload
Auth Guards in Angular 9/10/11
Angular | keyup event
What is AOT and JIT Compiler in Angular ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?