AngularJS Controllers

 In Uncategorized


The controllers are JavaScript functions that are responsible for handling user input and managing data within a particular section of an application. AngularJS controllers are regular JavaScript Objects.

Creation of controllers in AngularJS:

Define a module for an Application:

var myApp = angular.module(‘myApp’, []);

Create the Controller within the module:

myApp.controller(‘myController’, function($scope) {

// controller logic here


The ‘$scope’ parameter is used to communicate between the controller and the view.

Use of Controllers:

The controller can be used by ‘ng-controller’

<div ng-app=”myApp” ng-controller=”myController”>

<h1>{{ message }}</h1>



We defined a variable called ‘message’. In the view, we’re using AngularJS’s data binding syntax ({{ }}) to display the value of this variable.

User handling:

<div ng-app=”myApp” ng-controller=”myController”>

<input type=”text” ng-model=”name”>

<button ng-click=”sayHello()”>Say hello</button>

<p>{{ greeting }}</p>


When the user clicks the ‘Say hello’ button, AngularJS will call the ‘sayHello()’ function and update the ‘greeting’ variable. This will then be displayed in the view using data binding

Example code:

<!DOCTYPE html>


<script src=””></script>


<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>


Full Name: {{fullName()}}



var app = angular.module(‘myApp’, []);

app.controller(‘personCtrl’, function($scope) {

$scope.firstName = “cuben”;

$scope.lastName = “square”;

$scope.fullName = function() {

return $scope.firstName + “ “ + $scope.lastName;








Finally the controllers are a powerful feature of AngularJS that can be used to create dynamic and responsive web applications. By separating concerns and organizing code, controllers make it easier to manage complex applications and handle user input.

Recent Posts
Learn Devops

Become a Devops Engineer in 3 months