Introduction
AngularJS Controller is a JavaScript object that controls the data of AngularJS application. In Angular, Controller is defined by a JavaScript constructor function that is used to augment Angular scope.
The ng-controller directive defines the application controller. When a controller is attached to the DOM via ng-controller, the Angular will instantiate the new controller object using the specified controllers constructor function. A new child scope will be created and made available to the constructor function of specified controller as $scope.
We use controllers to set the initial state of $scope object and they can be used to add some behavior to $scope object. Controller must be used to contain only business logic.
Creating AngularJs Controllers
When we create an angular application, first we need to set up the initial state for Angular $scope. We can set up the initial state of a $scope by attaching properties to the $scope object. These properties contain view model. View model is a model that will be represented by the view. All these properties that are attached to the $scope will be available to the template at the point in the DOM where we have registered a controller. AngularJs invokes the controller with the $scope object.
Let us understand with an example: creating an Angular Controller.
Creating controller
- var myApp = angular.module('myApp', []);
- myApp.controller('MyFirstController', ['$scope', function($scope) {
- $scope.myvar = 'Hello, this is controller!';
- }]);
In this example, we have created a controller “MyFirstController”, that attaches the myvar property that contains string “MyFirstController” to $scope.
Here, we have created angular.module myApp, then we have added controllers constructor function to this module. This is done by using .controller() method. This keeps the constructor function out of global scope. After creating controller, we will create a view for this. We can do this like the below example :
View model( Data Binding)
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
- <script src="mycontroller.js"></script>
- </head>
-
- <body ng-app="myApp">
- <div ng-controller="MyFirstController"> {{myvar}} </div>
- </body>
-
- </html>
Here we have attached our controller to the DOM using ng-controller, like we have done below,
- <div ng-controller="MyFirstController"> {{myvar}} </div>
If we run this and verify the output, we will get the output as below,
Behavior to a scope object
We add methods to the $scope object to add some behavior to it. It will react to the events in the view if we have added some behavior to $scope.
Let’s add controller methods (variables and functions). Let’s understand this with an example where we input first name and Last name in the input fields and show full name by combining first name and last name.
First of all we will get the first name and last name into ng-models “firstName” and “lastName” respectively. The we will create a controller “nameController”, where we will pass firstname and lastname to $scope object. We will create $scope.fullName where we will combine firstname and lastname and return . This will be shown in view where we will access the fullName of the controller.
Example
View
- <div ng-app="myApp" ng-controller="nameController">
- First Name: <input type="text" ng-model="firstName"><br>
- Last Name: <input type="text" ng-model="lastName"><br>
- <br>
- Full Name: {{fullName()}}
- </div>
Controller
- var app = angular.module('myApp', []);
- app.controller('nameController', function($scope) {
- $scope.firstName = "Jasbeer ";
- $scope.lastName = "Singh";
- $scope.fullName = function() {
- return $scope.firstName + " " + $scope.lastName;
- };
- });
You can check here we have passed firstName and lastName to ng-models and then in the controller we have returned fullName by combining fristName and lastName of $scope object into $scope.fullName. We have used the nameController in the view using
- ng-controller=”nameController”
Now, if we run this we will get the output as,
First it will give the static result as we have provided Jasbeer as the first name and singh as the last name so fullName will be “Jasbeer singh”.
Now, if we change the text of first name and last name in input types as first name = Angular, last name =Js we will get output as,
Controllers should only contain the business logic. A controller is a JavaScript Object with properties and functions. Each Controller accepts $scope as a parameter which refers to the module or application that the controller is to control.
This was the basics of Angular JS Controller. Please provide your valuable feedback about this.