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) {  
-   
- } );  
 
- Controller define as JavaScript function
 
 We can also define controller as JavaScript function. In this function, we  	can also inject the dependency.
 - function controllerAsFunction($scope){  
-    $scope.myName = "Tejas Trivedi";  
- }  
 
 - <!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>  
 
 
 ![controller example]() 
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>  
![Scope Inheritance]() 
  In preceding example, we nested three ng-controller directives in the template.  All the controllers have same name property and all controllers hide their  parent properties. This works exactly as heritance works.  
Controller Alias 
  When we use "Controller as" syntax, we declare our controller in view with  alias. Here we use "Controller as alias" instead of injecting $scope in to our  controller. We can also use “controller as” in directive.  
Syntax   
 Inside the controller, declare function and variables on the "this" keyword  instead of binding them with the $scope.
 
 Example:
 
- <!DOCTYPE html>  
- <html>  
- <head>  
-     <title>Understanding Controllers and Services In AngularJS</title>  
-     <script src="angular.js"></script>  
- </head>  
- <body ng-app="myapp">  
-     <h4>Controller Alias Example</h4>  
-     <div>  
-         <div ng-controller="TestController as t1">  
-             <div> Hi, My name is {{t1.name}} </div>  
-         </div>  
-     </div>  
-     <script>  
-         var app = angular.module("myapp", []);  
-         app.controller("TestController", function () {  
-             this.name = "Jignesh Trivedi";  
-         });  
-     </script>  
- </body>  
- </html>  
![Controller Alias]() Define method(s) / function(s) in Controller
  Define method(s) / function(s) in Controller
  Controller can have functions and methods which are directly or indirectly  invoked by the view.  
Example:
 - <!DOCTYPE html>  
- <html>  
- <head>  
-     <title>Understanding Controllers and Services In AngularJS</title>  
-     <script src="angular.js"></script>  
- </head>  
- <body ng-app="myapp">  
-     <h4>Function / Method Example</h4>  
-     <div>  
-         <div ng-controller="TestController">  
-             <div> <button ng-click="clickMe()">Click me</button> </div>  
-         </div>  
-     </div>  
-     <script>  
-         var app = angular.module("myapp", []);  
-         app.controller("TestController", function ($scope) {  
-             $scope.clickMe = function () {  
-                 alert('Hi, Jignesh!!');  
-             };  
-         });  
-     </script>  
- </body>  
- </html>  
![Output]() Summary
  Summary
  Controller is a JavaScript function that contains all client side business logic.  It can communicate with DOM object.