Controller in AngularJS

Before reading this article I recommend you please go through the previous articles.

AngularJS Controller

To control the data of AngularJS, controllers are used. A Controller is basically a JavaScript constructor function, Angular will instantiate a new Controller object whenever we attach the ng-controller directive in the DOM.Constructor function as a JavaScript file, besides this file can be used to add an ng-directive to the HTML .

We used a parameter to this function that is $scope.

scope basically refers to the model organised in a hierarchy. We can group model variables inside a JavaScript function with a $scope argument.    
                                    
We defined our controller in the HTML with directive ng-controller, in which we provide the name of JavaScript variable that is defined in a JavaScript file.

Defining a Controller
  1. function ContactController($scope) {    
  2.     
  3. //...    
  4.     
  5. }   
Purpose of Controller: The Controller exposes the variables and functionality to directives and expressions.

In the following example, we will try to show how a Controller and $scope are used.

Code
  1. <!DOCTYPE html>    
  2. <html>    
  3.     
  4. <head>    
  5.     <title>Using $scope Object</title>    
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>    
  7. </head>    
  8. <body>    
  9.     <div ng-app="" ng-controller="employeeController">    
  10.         Emp Name: <input type="text" ng-model="name"><br>    
  11.         Department: <input type="text" ng-model="dept"><br>    
  12.         Position: <input type="text" ng-model="position"><br>    
  13.         <br>    
  14.         Details: {{name + " " + dept +" " + position }}    
  15.     </div>    
  16.     <script>    
  17.         function employeeController($scope) {    
  18.             $scope.name = "iShriss",    
  19.             $scope.dept = "IT",    
  20.             $scope.position = "developer"    
  21.         }    
  22.     </script>    
  23. </body>    
  24. </html>    
Let's check this in the Browser.



In the preceding example, we defined our application inside a <div> tag using a ng-app directive, followed by a ng-controller directive that defines our controller object. We have a standard JavaScript object constructor, employeeController, that is invoked by a $scope object.

Using Controller

In the preceding example we do have three variables created by employeeController. ng-model will bind those input fields to the controller variables. In the preceding example we used that controller object with three variables,we can also used methods as controller variable. Let's see an example.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Using Controller Method</title>  
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>  
  7. </head>  
  8. <body>  
  9.     <div ng-app="" ng-controller="employeeController">  
  10.         Emp Name: <input type="text" ng-model="name"><br>  
  11.         Department: <input type="text" ng-model="dept"><br>  
  12.         Position: <input type="text" ng-model="position"><br>  
  13.         <br>  
  14.         Details: {{details()}}  
  15.   
  16.     </div>  
  17.   
  18.     <script>  
  19.         function employeeController($scope) {  
  20.             $scope.name = "iShriss",  
  21.             $scope.dept = "IT",  
  22.             $scope.position = "developer",  
  23.             $scope.details = function () {  
  24.                 return $scope.name + " " + $scope.dept + " " + $scope.position;  
  25.             }  
  26.         }  
  27.     </script>  
  28. </body>  
  29. </html>  
Output



Demo example

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>AJS DemoController</title>  
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>  
  7. </head>  
  8. <body>  
  9.     <h1>AJS ControllerDemo</h1>  
  10.     <div ng-app="" ng-controller="employeeController">  
  11.         Emp Name: <input type="text" ng-model="employee.name"><br>  
  12.         Department: <input type="text" ng-model="employee.dept"><br>  
  13.         Position: <input type="text" ng-model="employee.position"><br>  
  14.         <br>  
  15.         Employee details: {{employee.Details()}}  
  16.     </div>  
  17.     <script>  
  18.         function employeeController($scope) {  
  19.             $scope.employee = {  
  20.                 name: "Adam",  
  21.                 dept: "Networking",  
  22.                 position: "admin",  
  23.                 Details: function () {  
  24.                     var empObj;  
  25.                     empObj = $scope.employee;  
  26.                     return empObj.name + " " + empObj.dept + " " + empObj.position;  
  27.                 }  
  28.             };  
  29.         }  
  30.     </script>  
  31.   
  32. </body>  
  33. </html>   
 

In the preceding demoExample, we have a controller, employeeController, that is defined as a JavaScript object having $scope as an argument. Since we know that a $scope basically refers to the application model that will consume the employeeController object.

The $scope.employee here is a property of the employeeController, $scope.employee has 3 properties declared namely name.dept and position. To return the details of a $scope.employee object we have a Details function there.

Alternatively we can have a constructor function in a separate JavaScript file. A controller object in a JavaScript file can refer to a HTML page using the ng-controller directive.

Output of Demo Example

There are more concepts in AngularJS controller like nested controller, inheritence in Controller. We will definitely touch on those topics in the future articles as well.

Conclusion

This article explained what AngularJS controllers are and how to define them. Also $scope was introduced that is basically used to bind the model values with the views.

So folks, AngularJS is very much fun, isn't it? Happy Coding!


Similar Articles