SlideShare a Scribd company logo
Controllers 
Controller is a JavaScript constructor function that is used to augment the Angular Scope. Controllers are the place 
where we define our application behaviors by defining properties and functions. 
$controller service is responsible for instantiating controllers. 
Use controllers to: 
 Set up the initial state of the $scope object. 
 Add behavior to the $scope object. 
Do not use controllers to: 
 Manipulate DOM — Controllers should contain only Application logic. Angular has databinding for most 
cases and directives to encapsulate manual DOM manipulation. 
 Format input — Use angular form controls instead. 
 Filter output — Use angular filters instead. 
 Share code or state across controllers — Use angular services instead. 
 Manage the life-cycle of other components (for example, to create service instances). 
Property Initialization in Controller 
Action declaration in Controller
NOTE: It is considered a best-practice to name our controllers as [Name]Controller, rather than [Name]Ctrl. 
Example 
external.js 
//defining module 
var app = angular.module('IG', []); 
//Action Method: increase 
//Action Method: decrease 
app.controller('FirstController', function ($scope) { 
$scope.counter = 0; 
$scope.add = function (amount) { $scope.counter += amount; }; 
$scope.subtract = function (amount) { $scope.counter -= amount; }; 
}); 
index.html 
<!DOCTYPE html> 
<html ng-app="IG"> 
<head> 
<title>AngularJS rootScope and scope :: InterviewGully.com</title> 
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> 
<script src="Scripts/external.js"></script> 
</head> 
<body> 
<div ng-controller="FirstController"> 
<h4>The simplest adding machine ever</h4> 
<button ng-click="add(1)" class="button">Increase</button> 
<button ng-click="subtract(1)" class="button alert">Decrease</button> 
<h4>Current count: {{ counter }}</h4> 
</div> 
</body> 
</html> 
Controller Hierarchy (Scopes within Scopes) 
By default, for any property that AngularJS cannot find on a local scope, AngularJS will crawl up to the containing 
(parent) scope and look for the property or method there. If AngularJS can’t find the property there, it will walk to that 
scope’s parent and so on and so forth until it reaches the Controllers $rootScope. 
If it doesn’t find it on the $rootScope, then it moves on and is unable to update the view. 
//defining module 
var app = angular.module('IG', []); 
//Property: person 
app.controller('ParentController', function ($scope) { 
$scope.person = { greeted: false }; 
}); 
//Action: sayHello
app.controller('ChildController', function ($scope) { 
$scope.sayHello = function () { 
$scope.person.name = "Ari Lerner"; 
$scope.person.greeted = true; 
} 
}); 
To see this behavior in action, let’s create a ParentController that contains the user object and a 
ChildController that wants to reference that object. 
If we bind the ChildController under the ParentController in our view, then the parent of the ChildController’s $scope 
object will be the ParentController’s $scope object. Due to the prototypal behavior, we can then reference data on the 
ParentController’s containing $scope on the child scope. 
<!DOCTYPE html> 
<html ng-app="IG"> 
<head> 
<title>AngularJS rootScope and scope :: InterviewGully.com</title> 
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> 
<script src="Scripts/external.js"></script> 
</head> 
<body> 
<div ng-controller="ParentController"> 
<div ng-controller="ChildController"> 
<button ng-click="sayHello()">Say hello</button> 
</div> 
{{ person }} 
</div> 
</body> 
</html> 
Sharing Data between Controller 
//defining module 
var app = angular.module('IG', []); 
//Property: person 
app.controller('FirstController', function ($scope,data) { 
$scope.person.name = data; 
}); 
//Property: person 
app.controller('SecondController', function ($scope,data) { 
$scope.person.name = data; 
});
//factory services 
app.factory('data', function () { 
return { 
Message: 'hey I am ur service' 
}; 
}); 
<!DOCTYPE html> 
<html ng-app="IG"> 
<head> 
<title>AngularJS rootScope and scope :: InterviewGully.com</title> 
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> 
<script src="Scripts/external.js"></script> 
</head> 
<body> 
<div ng-controller="FirstController"> 
{{ person }} 
</div> 
<div ng-controller="SecondController"> 
{{ person }} 
</div> 
</body> 
</html>

More Related Content

What's hot (20)

Why angular js Framework
Why angular js Framework
Sakthi Bro
 
Understanding angular js
Understanding angular js
Aayush Shrestha
 
Angular directive filter and routing
Angular directive filter and routing
jagriti srivastava
 
AngularJS Directives
AngularJS Directives
Eyal Vardi
 
Angular js
Angular js
ParmarAnisha
 
AngularJS in 60ish Minutes
AngularJS in 60ish Minutes
Dan Wahlin
 
Angular js routing options
Angular js routing options
Nir Kaufman
 
Angular js
Angular js
Behind D Walls
 
Angular js
Angular js
sanjay joshi
 
Introduction to AngularJS
Introduction to AngularJS
Jussi Pohjolainen
 
AngularJS: an introduction
AngularJS: an introduction
Luigi De Russis
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
Dan Wahlin
 
AngularJs presentation
AngularJs presentation
Phan Tuan
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
murtazahaveliwala
 
Practical AngularJS
Practical AngularJS
Wei Ru
 
Angular JS - Introduction
Angular JS - Introduction
Sagar Acharya
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
AngularJS Beginners Workshop
AngularJS Beginners Workshop
Sathish VJ
 
Dart and AngularDart
Dart and AngularDart
Loc Nguyen
 
Introduction to Angularjs
Introduction to Angularjs
Manish Shekhawat
 
Why angular js Framework
Why angular js Framework
Sakthi Bro
 
Understanding angular js
Understanding angular js
Aayush Shrestha
 
Angular directive filter and routing
Angular directive filter and routing
jagriti srivastava
 
AngularJS Directives
AngularJS Directives
Eyal Vardi
 
AngularJS in 60ish Minutes
AngularJS in 60ish Minutes
Dan Wahlin
 
Angular js routing options
Angular js routing options
Nir Kaufman
 
AngularJS: an introduction
AngularJS: an introduction
Luigi De Russis
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
Dan Wahlin
 
AngularJs presentation
AngularJs presentation
Phan Tuan
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
murtazahaveliwala
 
Practical AngularJS
Practical AngularJS
Wei Ru
 
Angular JS - Introduction
Angular JS - Introduction
Sagar Acharya
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
AngularJS Beginners Workshop
AngularJS Beginners Workshop
Sathish VJ
 
Dart and AngularDart
Dart and AngularDart
Loc Nguyen
 

Similar to Controller in AngularJS (20)

Angular js
Angular js
Arun Somu Panneerselvam
 
Step by Step - AngularJS
Step by Step - AngularJS
Infragistics
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto University
SC5.io
 
Angular workshop - Full Development Guide
Angular workshop - Full Development Guide
Nitin Giri
 
AngularJs-training
AngularJs-training
Pratchaya Suputsopon
 
Angular js
Angular js
Baldeep Sohal
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
introduction to Angularjs basics
introduction to Angularjs basics
Ravindra K
 
Basics of AngularJS
Basics of AngularJS
Filip Janevski
 
AngularJS Basic Training
AngularJS Basic Training
Cornel Stefanache
 
AngularJS Basics
AngularJS Basics
Ravi Mone
 
AngularJS Scopes
AngularJS Scopes
Mohamed Elkhodary
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
AngularJS Workshop
AngularJS Workshop
Gianluca Cacace
 
Scope in AngularJs
Scope in AngularJs
Thien To
 
Angular js
Angular js
Hritesh Saha
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
AngularJS.part1
AngularJS.part1
Andrey Kolodnitsky
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
Christoffer Noring
 
AngularJS
AngularJS
LearningTech
 
Step by Step - AngularJS
Step by Step - AngularJS
Infragistics
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto University
SC5.io
 
Angular workshop - Full Development Guide
Angular workshop - Full Development Guide
Nitin Giri
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
introduction to Angularjs basics
introduction to Angularjs basics
Ravindra K
 
AngularJS Basics
AngularJS Basics
Ravi Mone
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Scope in AngularJs
Scope in AngularJs
Thien To
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
Ad

Recently uploaded (20)

Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Ad

Controller in AngularJS

  • 1. Controllers Controller is a JavaScript constructor function that is used to augment the Angular Scope. Controllers are the place where we define our application behaviors by defining properties and functions. $controller service is responsible for instantiating controllers. Use controllers to:  Set up the initial state of the $scope object.  Add behavior to the $scope object. Do not use controllers to:  Manipulate DOM — Controllers should contain only Application logic. Angular has databinding for most cases and directives to encapsulate manual DOM manipulation.  Format input — Use angular form controls instead.  Filter output — Use angular filters instead.  Share code or state across controllers — Use angular services instead.  Manage the life-cycle of other components (for example, to create service instances). Property Initialization in Controller Action declaration in Controller
  • 2. NOTE: It is considered a best-practice to name our controllers as [Name]Controller, rather than [Name]Ctrl. Example external.js //defining module var app = angular.module('IG', []); //Action Method: increase //Action Method: decrease app.controller('FirstController', function ($scope) { $scope.counter = 0; $scope.add = function (amount) { $scope.counter += amount; }; $scope.subtract = function (amount) { $scope.counter -= amount; }; }); index.html <!DOCTYPE html> <html ng-app="IG"> <head> <title>AngularJS rootScope and scope :: InterviewGully.com</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> <script src="Scripts/external.js"></script> </head> <body> <div ng-controller="FirstController"> <h4>The simplest adding machine ever</h4> <button ng-click="add(1)" class="button">Increase</button> <button ng-click="subtract(1)" class="button alert">Decrease</button> <h4>Current count: {{ counter }}</h4> </div> </body> </html> Controller Hierarchy (Scopes within Scopes) By default, for any property that AngularJS cannot find on a local scope, AngularJS will crawl up to the containing (parent) scope and look for the property or method there. If AngularJS can’t find the property there, it will walk to that scope’s parent and so on and so forth until it reaches the Controllers $rootScope. If it doesn’t find it on the $rootScope, then it moves on and is unable to update the view. //defining module var app = angular.module('IG', []); //Property: person app.controller('ParentController', function ($scope) { $scope.person = { greeted: false }; }); //Action: sayHello
  • 3. app.controller('ChildController', function ($scope) { $scope.sayHello = function () { $scope.person.name = "Ari Lerner"; $scope.person.greeted = true; } }); To see this behavior in action, let’s create a ParentController that contains the user object and a ChildController that wants to reference that object. If we bind the ChildController under the ParentController in our view, then the parent of the ChildController’s $scope object will be the ParentController’s $scope object. Due to the prototypal behavior, we can then reference data on the ParentController’s containing $scope on the child scope. <!DOCTYPE html> <html ng-app="IG"> <head> <title>AngularJS rootScope and scope :: InterviewGully.com</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> <script src="Scripts/external.js"></script> </head> <body> <div ng-controller="ParentController"> <div ng-controller="ChildController"> <button ng-click="sayHello()">Say hello</button> </div> {{ person }} </div> </body> </html> Sharing Data between Controller //defining module var app = angular.module('IG', []); //Property: person app.controller('FirstController', function ($scope,data) { $scope.person.name = data; }); //Property: person app.controller('SecondController', function ($scope,data) { $scope.person.name = data; });
  • 4. //factory services app.factory('data', function () { return { Message: 'hey I am ur service' }; }); <!DOCTYPE html> <html ng-app="IG"> <head> <title>AngularJS rootScope and scope :: InterviewGully.com</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> <script src="Scripts/external.js"></script> </head> <body> <div ng-controller="FirstController"> {{ person }} </div> <div ng-controller="SecondController"> {{ person }} </div> </body> </html>