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

Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
Sakthi Bro
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
Aayush Shrestha
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
jagriti srivastava
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Angular js
Angular jsAngular js
Angular js
ParmarAnisha
 
AngularJS in 60ish Minutes
AngularJS in 60ish MinutesAngularJS in 60ish Minutes
AngularJS in 60ish Minutes
Dan Wahlin
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
Nir Kaufman
 
Angular js
Angular jsAngular js
Angular js
sanjay joshi
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Jussi Pohjolainen
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
Luigi De Russis
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
Dan Wahlin
 
AngularJs presentation
AngularJs presentation 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 AngularJSAngularJS 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 AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
Sagar Acharya
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
Sathish VJ
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDart
Loc Nguyen
 

What's hot (20)

Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS in 60ish Minutes
AngularJS in 60ish MinutesAngularJS in 60ish Minutes
AngularJS in 60ish Minutes
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDart
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 

Similar to Controller in AngularJS

Different way to share data between controllers in angular js
Different way to share data between controllers in angular jsDifferent way to share data between controllers in angular js
Different way to share data between controllers in angular js
codeandyou forums
 
Sharing Data between controllers in different ways.
Sharing Data between controllers in different ways.Sharing Data between controllers in different ways.
Sharing Data between controllers in different ways.
Amar Shukla
 
Angular js
Angular jsAngular js
Angular js
prasaddammalapati
 
Course CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.jsCourse CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.js
Vinícius de Moraes
 
Starting with angular js
Starting with angular js Starting with angular js
Starting with angular js
jagriti srivastava
 
Angular js
Angular jsAngular js
Angular js
Eueung Mulyana
 
intro to Angular js
intro to Angular jsintro to Angular js
intro to Angular js
Brian Atkins
 
243329387 angular-docs
243329387 angular-docs243329387 angular-docs
243329387 angular-docs
Abhi166803
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
Dariusz Kalbarczyk
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
Filip Janevski
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
Ravindra K
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
RajthilakMCA
 
Custom directive and scopes
Custom directive and scopesCustom directive and scopes
Custom directive and scopes
jagriti srivastava
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for Beginners
Oswald Campesato
 
Client-side Rendering with AngularJS
Client-side Rendering with AngularJSClient-side Rendering with AngularJS
Client-side Rendering with AngularJS
David Lapsley
 
Angular Js Basics
Angular Js BasicsAngular Js Basics
Angular Js Basics
أحمد عبد الوهاب
 
[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes
Globant
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
SolTech, Inc.
 

Similar to Controller in AngularJS (20)

Different way to share data between controllers in angular js
Different way to share data between controllers in angular jsDifferent way to share data between controllers in angular js
Different way to share data between controllers in angular js
 
Sharing Data between controllers in different ways.
Sharing Data between controllers in different ways.Sharing Data between controllers in different ways.
Sharing Data between controllers in different ways.
 
Angular js
Angular jsAngular js
Angular js
 
Course CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.jsCourse CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.js
 
Starting with angular js
Starting with angular js Starting with angular js
Starting with angular js
 
Angular js
Angular jsAngular js
Angular js
 
intro to Angular js
intro to Angular jsintro to Angular js
intro to Angular js
 
243329387 angular-docs
243329387 angular-docs243329387 angular-docs
243329387 angular-docs
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
 
Custom directive and scopes
Custom directive and scopesCustom directive and scopes
Custom directive and scopes
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for Beginners
 
Client-side Rendering with AngularJS
Client-side Rendering with AngularJSClient-side Rendering with AngularJS
Client-side Rendering with AngularJS
 
Angular Js Basics
Angular Js BasicsAngular Js Basics
Angular Js Basics
 
[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 

Recently uploaded (20)

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 

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>