In this Article, we will see the role of Modules, Controllers, $scope in AngularJS Application.
In previous AngularJS Tutorials Series, We saw
Module: Module in AngularJS applications is a container for controllers, directive, filters, services etc and helps in packaging code as reusable modules.
Creating/Defining a Module:
The first parameter in angular.module() function is the name of the module and the second parameter is an array in which we can add dependencies. Here, we have not added any external modules/Dependencies as we are trying to make this example as simple as possible.
In the above code, We have added a controller with our angular module(i.e. myApp) using Controller method of the module. In Controller method, the first parameter is name of the controller and second is function representing the controller.
$scope acts as a glue between application controller and the view. $scope is dynamically injected into controller's function. We have added some data to $scope properties.
Here, we have added our module and controller in View using ng-App and ng-controller directive of AngularJS.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Working with Controller</title>
<script src="Script/angular.js"></script>
<script>
// Declare a module
var myApp = angular.module('myApp', []);
//Registering a controller in myApp module
myApp.controller('myController', function ($scope) {
$scope.Name = "Anoop Kumar Sharma";
$scope.Website = "www.ittutorialswithexample.com";
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
Name:{{Name}}<br />
Website:{{Website}}
</div>
</body>
</html>
|
Preview:
Example2.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Working with Controller</title>
<script src="Script/angular.js"></script>
<script src="Script/controller.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
Name:{{Name}}<br />
Website:{{Website}}
</div>
</body>
</html>
|
// Declare a module
var myApp = angular.module('myApp', []);
//Registering a controller in myApp module
myApp.controller('myController', function ($scope) {
$scope.Name = "Anoop Kumar Sharma";
$scope.Website = "www.ittutorialswithexample.com";
});
|
Hope you like it. Thanks.
[Download Source Code via Google Drive]








This article gives a clear introduction to the core building blocks of an AngularJS application by explaining how modules, controllers, and $scope work together. The examples make the relationship between the JavaScript code and the HTML view easy to understand, particularly through ng-app, ng-controller, and the data stored in $scope.
ReplyDeleteThe explanation of creating a module and registering a controller is a useful starting point for Angular Training, especially for understanding how framework-based applications organize their code. Moving the controller logic into a separate controller.js file also demonstrates a simple way to keep the application structure cleaner.
The article also reinforces important JavaScript concepts because the AngularJS module and controller are defined using JavaScript functions and objects. This makes the example relevant to Javascript Training, while the separation between the view and controller provides a useful foundation for broader Frontend Training and client-side application development.
ReplyDelete