Introduction
In my previous article “AngularJS Overview” and "Data Binding in AngularJS", we saw an overview of AngularJS features, a hello world application and data binding in AngularJS. This article explains controllers in AngularJS.
AngularJS Controllers
Controller in AngularJS is nothing but JavaScript constructor function that uses the Angular scope. It also controls the data of the application. Controller function is attached with DOM object. The ng-controller directive helps to define controller. When the ng-controller directive find by the AngularJS application, it will instantiate a new Controller object and create a new child scope ($scope) and it made available as an injectable parameter (to the Controller's constructor function). Controller is heart of AngularJS application and it mainly contains the business logic, so it is not good idea to put presentation logic into Controllers, it may affect controller's testability.
We can define controller in the following two ways:
- Define Controller as application module
Normally angular developer uses this approach. In this approach, angular application has been defined and controller has been added in this angular application.
- angular.module("myapp", [])
- .controller("appController", function($scope) {
- // definition of controller
- } );
- Controller define as JavaScript function
We can also define controller as JavaScript function. In this function, we can also inject the dependency.
Full example:- function controllerAsFunction($scope){
- $scope.myName = "Tejas Trivedi";
- }
Output:- <!DOCTYPE html>
- <html>
- <head>
- <title>Understanding Controllers and Services In AngularJS</title>
- <script src="angular.js"></script>
- </head>
- <body ng-app="myapp">
- <h4>Controller Example</h4>
- <br/>
- <br/>
- <div>1) Application.controller</div>
- <div ng-controller="appController">
- <div> My Name (app.contrller): {{myName}} </div>
- </div>
- <br/>
- <br/>
- <div>2) Controller as function</div>
- <div ng-controller="controllerAsFunction">
- <div> My Name (controller as function): {{myName}} </div>
- </div>
- <script>
- angular.module("myapp", [])
- .controller("appController", function($scope) {
- $scope.myName = "Jignesh Trivedi";
- });
- function controllerAsFunction($scope) {
- $scope.myName = "Tejas Trivedi";
- }
- </script>
- </body>
- </html>

Scope Inheritance
As described earlier, the ng-controller directive always creates a new child scope, so that we can get a hierarchy of scopes that inherit from each other. Each $scope variable of the controller will have access to the properties and methods define by the controllers.
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Understanding Controllers and Services In AngularJS</title>
- <script src="angular.js"></script>
- </head>
- <body ng-app="myapp">
- <h4>Scope Inheritance Example</h4>
- <div>
- <div ng-controller="ParentController" style="border: solid 2px red; padding: 15px;">
- <div> Hi, My name is {{name}} </div>
- <div ng-controller="firstChildController" style="border: solid 2px red; padding: 15px;">
- <div> Hi, My name is {{name}} </div>
- <div ng-controller="secondChildController" style="border: solid 2px red; padding: 15px;">
- <div> Hi, My name is {{name}} </div>
- </div>
- </div>
- </div>
- </div>
- <script>
- var app = angular.module("myapp", []);
- app.controller("ParentController", function ($scope) {
- $scope.name = "Jignesh Trivedi";
- });
- app.controller("firstChildController", function ($scope) {
- $scope.name = "Tejas Trivedi";
- });
- app.controller("secondChildController", function ($scope) {
- $scope.name = "Rakesh Trivedi";
- });
- </script>
- </body>
- </html>




Rajeev PunhaniPosted Mar 30, 2016, 4:21 AM
Nice.
raj krishnaPosted Jan 19, 2016, 5:17 AM
good article
Harshad PansuriyaPosted Sep 23, 2015, 3:30 AM
Nice One
Sibeesh VenuPosted Sep 23, 2015, 3:07 AM
Nice Share
Ajeet MishraPosted Sep 23, 2015, 2:27 AM
nice
Muhammad Aqib ShehzadPosted Sep 23, 2015, 1:57 AM
Good article
Ankit BansalPosted Sep 23, 2015, 1:52 AM
thanks for sharing..
Karthikeyan KPosted Sep 22, 2015, 11:23 PM
Good one...
Santhakumar MunuswamyPosted Sep 22, 2015, 3:04 PM
Nice Article. Thanks for sharing
Anil KumarPosted Sep 22, 2015, 10:11 AM
.