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

DOCX
Built in filters
DOCX
Understanding angular js $rootscope and $scope
DOCX
Shaping up with angular JS
DOCX
Directives
PPTX
AngularJS Introduction
DOCX
Filters in AngularJS
DOCX
multiple views and routing
PPTX
Angular js PPT
Built in filters
Understanding angular js $rootscope and $scope
Shaping up with angular JS
Directives
AngularJS Introduction
Filters in AngularJS
multiple views and routing
Angular js PPT

What's hot (20)

PPTX
Why angular js Framework
PPTX
Understanding angular js
PPTX
Angular directive filter and routing
PPTX
AngularJS Directives
PPTX
Angular js
PPTX
AngularJS in 60ish Minutes
PDF
Angular js routing options
PPTX
Angular js
PPTX
Angular js
PDF
Introduction to AngularJS
PDF
AngularJS: an introduction
PPTX
Building an End-to-End AngularJS Application
PPTX
AngularJs presentation
PPTX
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
PPTX
Practical AngularJS
PPTX
Angular JS - Introduction
PDF
Advanced Tips & Tricks for using Angular JS
PPTX
AngularJS Beginners Workshop
PPTX
Dart and AngularDart
PPTX
Introduction to Angularjs
Why angular js Framework
Understanding angular js
Angular directive filter and routing
AngularJS Directives
Angular js
AngularJS in 60ish Minutes
Angular js routing options
Angular js
Angular js
Introduction to AngularJS
AngularJS: an introduction
Building an End-to-End AngularJS Application
AngularJs presentation
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
Practical AngularJS
Angular JS - Introduction
Advanced Tips & Tricks for using Angular JS
AngularJS Beginners Workshop
Dart and AngularDart
Introduction to Angularjs
Ad

Similar to Controller in AngularJS (20)

DOCX
Sharing Data between controllers in different ways.
DOCX
Different way to share data between controllers in angular js
DOCX
Angular js
PDF
Course CodeSchool - Shaping up with Angular.js
PPTX
Starting with angular js
PDF
Angular js
PPTX
intro to Angular js
PDF
243329387 angular-docs
PPT
AngularJS Mobile Warsaw 20-10-2014
PDF
Introduction to AngularJS
PPTX
Basics of AngularJS
PPTX
lec 14-15 Jquery_All About J-query_.pptx
PPT
introduction to Angularjs basics
PPTX
Building a dashboard using AngularJS
PPTX
Custom directive and scopes
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
PPTX
Angular1x and Angular 2 for Beginners
PPTX
Client-side Rendering with AngularJS
PPTX
Angular Js Basics
PPTX
Intro to AngularJs
Sharing Data between controllers in different ways.
Different way to share data between controllers in angular js
Angular js
Course CodeSchool - Shaping up with Angular.js
Starting with angular js
Angular js
intro to Angular js
243329387 angular-docs
AngularJS Mobile Warsaw 20-10-2014
Introduction to AngularJS
Basics of AngularJS
lec 14-15 Jquery_All About J-query_.pptx
introduction to Angularjs basics
Building a dashboard using AngularJS
Custom directive and scopes
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Angular1x and Angular 2 for Beginners
Client-side Rendering with AngularJS
Angular Js Basics
Intro to AngularJs
Ad

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Business Ethics Teaching Materials for college
PDF
Classroom Observation Tools for Teachers
PDF
Basic Mud Logging Guide for educational purpose
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
master seminar digital applications in india
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Institutional Correction lecture only . . .
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Pre independence Education in Inndia.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Insiders guide to clinical Medicine.pdf
Final Presentation General Medicine 03-08-2024.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Business Ethics Teaching Materials for college
Classroom Observation Tools for Teachers
Basic Mud Logging Guide for educational purpose
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
master seminar digital applications in india
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Institutional Correction lecture only . . .
Microbial diseases, their pathogenesis and prophylaxis
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharma ospi slides which help in ospi learning
Pre independence Education in Inndia.pdf
Microbial disease of the cardiovascular and lymphatic systems
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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>