Write a sample program using AngularJS Controllers ?

AngularJS application mainly relies on controllers to control the flow of data in the application. An AngularJS 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.

The ng-controller directive defines the application controller.

A controller is a JavaScript Object, created by a standard JavaScript object constructor.

AngularJS Controllers
<div ng-app=”myApp” ng-controller=”mySampleCtrl”>
First Name:  <input type=”text” ng-model=”userFirstName“><br>
Last Name: <input type=”text” ng-model=”userLastName“><br>
<br>
Full Name: {{userFirstName+ ” ” + userLastName}}</div>
<script>
var app = angular.module(‘myApp’, []);
app.controller(‘mySampleCtrl’, function($scope) {
$scope.userFirstName= “Vipin”;
$scope.userLastName= “Roy”;
});
</script>

The AngularJS application is defined by ng-app=”myApp“. The application runs inside the

The ng-controller=”myCtrl” attribute is an AngularJS directive. It defines a controller.

The mySampleCtrl 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 (userFirstName and userLastName).

The ng-model directives bind the input fields to the controller properties (userFirstName and userLastName).