Controllers

AngularJS application depends on controllers to control the flow of data in the application

app.controller("myController",function($scope)
{
$scope.name="";
});

A controller is a JavaScript object that contains attributes/properties, and functions. Each controller accepts $scope as a parameter, which refers to the application/module that the controller needs to handle

we give to each controller $scope as a parameter while we define them.$scope in turn refers to the module that controlller is to control.


<div ng-app = "" ng-controller = "studentController">
...
</div>
<html >
<head>
<title>AngularJS </title>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
</head>
<body>
<h2>Sample Application</h2>
<div ng-app = "mainApp" ng-controller = "studentController">
Enter first name: <input type = "text" ng-model = "student.firstName">
Enter last name: <input type = "text" ng-model = "student.lastName">
You are entering: {{student.fullName()}}
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.student = {
firstName: "khushi",
lastName: "kumari",
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});
</script>
</body>
</html>