AngularJs Tutorials
Important News
What is controller in AngularJs
A controller is defined using ng-controller directive. A controller is a JavaScript object containing attributes/properties and functions. Each controller accepts $scope as a parameter which refers to the application/module that controller is to control.
AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.
The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<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 + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "kalam";
$scope.lastName = "Habib";
});
</script>
</body>
</html>
Application explained:
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller.
The myCtrl function is a JavaScript function.
AngularJS will invoke the controller with a $scope object.
In AngularJS, $scope is the application object (the owner of application variables and functions).
The controller creates two properties (variables) in the scope (firstName and lastName).
The ng-model directives bind the input fields to the controller properties (firstName and lastName).
Controller Methods
The example above demonstrated a controller object with two properties: lastName and firstName. A controller can also have methods (variables as functions):
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($obj) {
$obj.firstName = "Mahtab";
$obj.lastName = "Nusrat";
$obj.fullName = function() {
return $obj.firstName + " " + $obj.lastName;
};
});
</script>
</body>
</html>
Controllers In External Files
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script src="personController.js"></script>
</body>
</html>
File name : personController.js
File name : index.php
<html>
<head>
<title>Angular JS Controller</title>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app = "mainApp" ng-controller = "studentController">
Enter first name: <input type = "text" ng-model = "student.firstName"><br><br>
Enter last name: <input type = "text" ng-model = "student.lastName"><br>
<br>
You are entering: {{student.fullName()}}
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.student = {
firstName: "Mahesh",
lastName: "Parashar",
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});
</script>
</body>
</html>
Controller :-
Controllers is a JavaScript constructor functions which are bound to a particular scope.
With angular.controller method controller is defined which allows us to bind controller to the module. It takes controller name and a function.
Define ng-controller='lang' on a <div> and in the <div> added {{ language }}. The value of language is assigned from the controller function.
To assign value in language use $scope.
$scope.language = "AngularJS";
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Controller example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js" type="text/javascript"></script>
</head>
<body ng-app="myLanguageApp">
<div ng-controller="lang">
{{ language }}
</div>
<script>
var application = angular.module('myLanguageApp',[]);
var myController = function($scope){
$scope.language = "AngularJS";
};
application.controller("lang",myController);
</script>
</body>
</html>
Using controller with ng-click
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Controller example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js" type="text/javascript"></script>
</head>
<body ng-app="myLanguageApp">
<div ng-controller="languages">
Select your Favourite Lanuage :
<button ng-click="lang('php programming')">PHP</button>
<button ng-click="lang('javascript')">Javascript</button>
<button ng-click="lang('python')">Python</button>
<button ng-click="lang('ruby')">Ruby</button>
<p>You have selected : {{ myFavLanguage }}</p>
</div>
<script>
var application = angular.module('myLanguageApp',[]);
application.controller('languages',function($scope){
$scope.myFavLanguage = "None";
$scope.lang = function(value){
$scope.myFavLanguage = value;
}
});
</script>
</body>
</html>