AngularJS Controllers

AngularJS Controllers control the data of AngularJS applications. AngularJS controllers are regular JavaScript Objects.

Sample Code:

<!DOCTYPE html>
<html>
<script src= “http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”></script>
<body>
<div ng-app=”myApp” ng-controller=”SampleCtrl”>
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(‘SampleCtrl’, function($scope) {
$scope.firstName = “Firstname”;
$scope.lastName = “Lastname”;
$scope.fullName = function() {
return $scope.firstName + ” ” + $scope.lastName;
}
});
</script>
</body>
</html>

Output:

Cheers!!!

Leave a Reply