How to Use Modules in Angularjs?

A module in AngularJS is a container of different parts of an application like controllers, services, filters, directives, factories etc. In AngularJS, some functionality of JavaScript can be grouped together under a single module.

In Angularjs, you can define your controllers, services, filters, instructions, etc. from a module, which will be accessible throughout the module.

A module can be used by AngularJS to bootstrap an application. By passing the module name to the ng application directive, we can inform AngularJS to load this module as the main entry point of the application. .

Modules are defined as separate .js files and names in module.js file How to create module See the example below

var mainApp = angular.module("mainApp", []);

Example Code

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "Rahul";
$scope.lastName = "Chaurasiya";
});
</script>
</body>
</html>

Example Result

Rahul Chaurasiya