Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Controllers – AngularJS

AngularJS supports Model-View-Controller (MVC) architecture pattern.

A Controller is a javascript object which has properties and functions.

AngularJS Controller is the only object that communicates with both Models and Views.


Controller Directive

In AngularJS application, a controller directive is defined with ng-controller directive.

<div ng-controller="empController">

Controller Syntax

<script>
	angular.module('empApp', []).controller('empController', ['$scope', function($scope)
	{
		$scope.name = '';
		$scope.getName = function() {
			$scope.greeting = 'Hello ' + $scope.name;
		}
	}]);
</script>
  • Create new module empApp using the angular.module function.
  • Create empController controller using the empApp module object.
  • Pass $scope as an argument to controller function. $scope is an object that refers to the application model.
  • Create name property on the controller object.
  • Create getName function on the controller object.

as Syntax

You can create alias for a controller using “as”.

To use an alias with the controller, you must use “this” instead of $scope parameter.

<body ng-app="empApp">
	<div ng-controller="empController as mainCtrl">
		<span>{{ mainCtrl.name }}
	</div>
	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js" ></script>
<script>
	angular.module('empApp', []).controller('empController', [function()
	{
		this.name = 'James';
	}]);
</script>

Controller Inheritance

A controller can be nested into another controller.

A child controller can inherit properties and functions from their parent controller.

<body ng-app="empApp">
	<div ng-controller="mainCtrl">
		<p>{{ name }}</p>
		<p>{{ greeting() }}</p>
		<div ng-controller="empCtrl">
			<p>{{ name }} </p>
			<p>{{ Age }} </p>
			<p>{{ greeting() }} </p>
		</div>
		<div ng-controller="mgrCtrl">
			<p>{{ name }} </p>
			<p>{{ position }} </p>
			<p>{{ greeting() }} </p>
		</div>
	</div>
	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js" ></script>
<script>
	var app = angular.module('empApp', []);
	
	//main controller
	app.controller('mainCtrl', ['$scope', function($scope){
		$scope.name = 'James';
		$scope.greeting = function() {
			return "Hello " + name;
		};
	}]);
	
	//employee controller
	app.controller('empCtrl', ['$scope', function($scope) {
		$scope.age = 24;
	}]);
	
	//manager controller
	app.controller('mgrCtrl',['$scope', function($scope) {
		$scope.position = 'manager';
	}]);
</script>
</body>
  • Create three controller mainCtrl, empCtrl, mgrCtrl.
  • empCtrl and mgrCtrl are the child controllers of mainCtrl.
  • mainCtrl contains one property name and a single function greeting.
  • empCtrl inherit both name and greeting function from the mainCtrl.
  • mgrCtrl inherit both name and greeting function from the mainCtrl.
  • empCtrl introduce new property age.
  • mgrCtrl introduce new property position.