What is the use of AngularJS Modules ?

An AngularJS module defines an application. AngularJS supports modular approach. Modules are used to separate logics say services, controllers, application etc. and keep the code clean. We define modules in separate js files and name them as per the module.js file. In this example we’re going to create two modules.

Application Module − used to initialize an application with controller(s).

Controller Module − used to define the controller.

The module is a container for the different parts of an application.The module is a container for the application controllers. Controllers always belong to a module.

This application (“myApp”) has one controller (“mySampleCtrl“):

Module With single Controller

<div ng-app=”myApp” ng-controller=”mySampleCtrl“>
{{ userFirstName + ” ” + userLastName }}
</div>

<script>
var app = angular.module(“myApp”, []);
app.controller(“mySampleCtrl“, function($scope) {
$scope.userFirstName = “John”;
$scope.userLastName = “Doe”;
});