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)

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

Similar to Controller in AngularJS (20)

DOCX
Sharing Data between controllers in different ways.
Amar Shukla
 
DOCX
Different way to share data between controllers in angular js
codeandyou forums
 
DOCX
Angular js
prasaddammalapati
 
PDF
Course CodeSchool - Shaping up with Angular.js
Vinícius de Moraes
 
PPTX
Starting with angular js
jagriti srivastava
 
PDF
Angular js
Eueung Mulyana
 
PPTX
intro to Angular js
Brian Atkins
 
PDF
243329387 angular-docs
Abhi166803
 
PPT
AngularJS Mobile Warsaw 20-10-2014
Dariusz Kalbarczyk
 
PDF
Introduction to AngularJS
Marco Vito Moscaritolo
 
PPTX
Basics of AngularJS
Filip Janevski
 
PPTX
lec 14-15 Jquery_All About J-query_.pptx
MuhammadAbubakar114879
 
PPT
introduction to Angularjs basics
Ravindra K
 
PPTX
Building a dashboard using AngularJS
RajthilakMCA
 
PPTX
Custom directive and scopes
jagriti srivastava
 
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
PPTX
Angular1x and Angular 2 for Beginners
Oswald Campesato
 
PPTX
Client-side Rendering with AngularJS
David Lapsley
 
PPTX
Angular Js Basics
أحمد عبد الوهاب
 
PPTX
Intro to AngularJs
SolTech, Inc.
 
Sharing Data between controllers in different ways.
Amar Shukla
 
Different way to share data between controllers in angular js
codeandyou forums
 
Angular js
prasaddammalapati
 
Course CodeSchool - Shaping up with Angular.js
Vinícius de Moraes
 
Starting with angular js
jagriti srivastava
 
Angular js
Eueung Mulyana
 
intro to Angular js
Brian Atkins
 
243329387 angular-docs
Abhi166803
 
AngularJS Mobile Warsaw 20-10-2014
Dariusz Kalbarczyk
 
Introduction to AngularJS
Marco Vito Moscaritolo
 
Basics of AngularJS
Filip Janevski
 
lec 14-15 Jquery_All About J-query_.pptx
MuhammadAbubakar114879
 
introduction to Angularjs basics
Ravindra K
 
Building a dashboard using AngularJS
RajthilakMCA
 
Custom directive and scopes
jagriti srivastava
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Angular1x and Angular 2 for Beginners
Oswald Campesato
 
Client-side Rendering with AngularJS
David Lapsley
 
Angular Js Basics
أحمد عبد الوهاب
 
Intro to AngularJs
SolTech, Inc.
 
Ad

Recently uploaded (20)

PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Controller Request and Response in Odoo18
Celine George
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
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>