AngularJS Controllers
In this article, we will see the Controller in AngularJS along with knowing how Controller works, the concept of the Controller method & how the Controller can be implemented in an external. We will understand all these aspects with the help of their implementation & accordingly will its illustrations.
AngularJS controllers play a significant role in AngularJS applications. All the AngularJS application mainly relies on the controllers to control the flow of data in that application. Basically, it controls the data of AngularJS applications and the controller is a Javascript object, created by a standard JavaScript object constructor.
The ng-controller directive defines the application controller. In AngularJS, a controller is defined by a Javascript construction function, which is used in AngularJS scope and also the function $scope) is defined when the controller is defining and it returns the concatenation of the $scope.firstname and $scope.lastname.
Syntax:
<element ng-controller="expression"> Contents... </element>
Example 1: This example implements the ng-controller Directive to display the concatenation of the First and Second input of a user.
HTML
<!DOCTYPE html> < html > < head > < title >ng-controller Directive</ title > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" > </ script > < script > var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.firstName = "Beginner"; $scope.lastName = "Beginner"; }); </ script > </ head > < body > < center > < h1 style = "color:green" >w3wiki</ h1 > < h2 >ng-controller Directive</ h2 > < div ng-app = "myApp" ng-controller = "myCtrl" > First Name: < input type = "text" ng-model = "firstName" > < br > Last Name: < input type = "text" ng-model = "lastName" > < br >< br > Full Name: {{firstName + "for" + lastName}} </ div > </ center > </ body > </ html > |
Output:
Controller method: This first example demonstrated a controller object with two properties: Fname and Lname. In AngularJS, the function($scope) is defined when controller is defining and it’s return the concatenation of the $scope.firstname and $scope.lastname. Here a controller can also have methods (variables as functions):
Example 2: This example illustrates the basic implementation of the Controller method in AngularJS.
HTML
<!DOCTYPE html> < html > < head > < title >ng-controller Directive</ title > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" > </ script > < script > var app = angular.module('Beginner', []); app.controller('personCtrl', function($scope) { $scope.fullName = function() { return $scope.firstName + " " + $scope.lastName; }; }); </ script > </ head > < body style = "padding: 30px" > < center > < div ng-app = "Beginner" ng-controller = "personCtrl" > < h1 style = "color:green" >w3wiki</ h1 > < h2 >ng-controller Directive</ h2 > First Name: < input type = "text" ng-model = "firstName" > < br >< br > Last Name: < input type = "text" ng-model = "lastName" > < br >< br > Full Name: {{fullName()}} </ div > </ center > </ body > </ html > |
Output:
Controllers in External files: In larger applications, it is common to store controllers in an external file, you just need to copy that file name and paste it into a <script> tag. Here the external file name is “script.js”
Example 3: This example illustrates defining the separate external file to define the controller in AngularJS.
HTML
<!DOCTYPE html> < html > < head > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" > </ script > < script src = "script.js" ></ script > </ head > < body > < center > < h1 style = "color:green" > w3wiki </ h1 > < h2 >ng-controller Directive</ h2 > < div ng-app = "myApp" ng-controller = "gfgCtrl" > First Name: < input type = "text" ng-model = "firstName" > < br > Last Name: < input type = "text" ng-model = "lastName" > < br >< br > Full Name: {{fullName()}} </ div > </ center > </ body > </ html > |
script.js:
Javascript
angular.module( 'myApp' , []).controller( 'gfgCtrl' , function ($scope) { $scope.firstName = "w3wiki" , $scope.lastName = "Learning Together" , $scope.fullName = function () { return $scope.firstName + " " + $scope.lastName; } }); |
Output:
Contact Us