SlideShare une entreprise Scribd logo
1  sur  21
(DI) is a software design pattern that 
deals with how components get hold of their dependencies. 
The Angular injector subsystem is in charge 
of creating components, their , 
and them to other components as requested.
Angular modules have the opportunity to configure 
themselves before the module actually bootstraps and 
starts to run. 
This phase is the only part 
of the Angular flow that can 
modify things before the 
app starts up. 
The only services that can 
be injected in this block 
are and ; 
Executed at begining of the 
application; 
Similar with the 
method in other 
programming languages; 
Any service can be injected 
here.
singleton objects that are instantiated only once per 
application; 
lazy-loaded (created only when necessary); 
provide a way to share data and behavior across 
controllers, directives, filters or other services; 
How to create a service? 
Build your own DI system 
.constant(); 
.value(); 
.service(); 
.factory(); 
.provider();
used for registering a constant service such as string, 
number, array, object or function; 
doesn't have the ability to use other services (have 
dependencies); 
angular 
.module('myApp', []) 
.constant('apiUrl', 'http://localhost:8080') 
.config(['apiUrl', function(apiUrl){ 
//apiUrl can be used here 
}]) 
.run(['$rootScope', function($rootScope){ 
//apiUrl can be used here 
}]);
used for registering a value service such as string, 
number, array, object or function; 
doesn't have the ability to use other services (have 
dependencies); 
angular 
.module('myApp', []) 
.value('objectValue', { 
foo: 'bar' 
}) 
.run(['$rootScope', 'objectValue', 
function($rootScope, objectValue){ 
$rootScope.foo = objectValue.foo; 
} 
]);
used for registering a 
service factory which will be 
to return the 
; 
ability to use other services 
(have dependencies); 
service initialization; 
delayed/lazy initialization. 
angular 
.module('myApp', []) 
.factory('myFactory', function(){ 
var data; //private variable 
return { 
fetchData: function(){ 
//business to populate data 
}, 
getData: function(){ 
return data; 
} 
} 
}) 
.run(['$rootScope', 'myFactory', 
function($rootScope, myFactory){ 
myFactory.fetchData(); 
$rootScope.data = myFactory.getData(); 
} 
]);
used for registering a 
service constructor which 
will be invoked 
with to create the 
; 
same as factory; 
angular 
.module('myApp', []) 
.service('myService', function(){ 
var data; //private variable 
this.fetchData= function(){ 
//business to populate data 
}; 
this.getData= function(){ 
return data; 
}; 
}); 
.factory('myService', function(){ 
var Service = function(){ 
var data; //private variable 
this.fetchData= function(){ 
//business to populate data 
}; 
this.getData= function(){ 
return data; 
}; 
}; 
return new Service(); 
});
, whose instance is responsible for 
' ' a ; 
can have additional methods that allow configuration 
of the provider or it's returning service; 
must have a : 
that returns the factory service; 
ability to use other services (have dependencies);
angular 
.module('myApp', []) 
.provider('myFactory', function(){ 
var configVar = 'value'; 
//The factory Service - can have any dependency 
this.$get = ['$http', function($http){ 
var data; //private variable 
return{ 
fetchData: function(){ 
//business to populate data 
}, 
getData: function(){ 
return data; 
} 
}; 
}]; 
//Config method 
this.config = function(config){ 
configVar = config; 
}; 
}) 
.config(['myFactoryProvider', function(myFactoryProvider){ 
myFactoryProvider.config('Overriden value'); 
}]) 
.run(['$rootScope', 'myFactory', 
function($rootScope, myFactory){ 
myFactory.fetchData(); 
$rootScope.data = myFactory.getData() 
} 
]);
Angular comes with several built-in services like: 
$http / $httpProvider; 
$compile / $compileProvider; 
many more. 
The is a convention to point that the service comes 
from the framework and it is not custom-made;
All the providers (services) are . 
That means that they are all ; 
A constant is a value that can be injected everywhere. 
The value of a constant can never be changed; 
A value is just a simple injectable value; 
A service is an injectable constructor; 
A factory is an injectable function; 
A provider is a configurable factory.
Example Link
module is a core Angular module for basic routing. 
The module provides the directive, in order to render 
the appropriate template. 
Any time the route is changed the directive will update it's 
content: 
if there is a template associated with the current route: 
link the controller (if specified) with the scope; 
link the new scope with the new template; 
remove the last view and clean the last scope; 
create a new scope - inherited from the parent;
To create routes on a specific module or app, 
exposes the . 
To add a specific route, has the 
method 
$routeProvider 
.when('path', { 
template: '', 
templateUrl: '', 
controller: '', 
controllerAs: '' 
//... 
}) 
.otherwise(routeConfigObj);
<html ng-app="myApp"> 
<head>...</head> 
<body> 
<header> 
<h1>My app</h1> 
<ul> 
<li><a href="#/">Home</a></li> 
<li><a href="#/about">About</a></li> 
</ul> 
</header> 
<div class="content"> 
<div ng-view></div> 
</div> 
</body> 
</html> 
angular 
.module('myApp', ['ngRoute']) 
.config(['$routeProvider', function($routeProvider){ 
$routeProvider 
.when('/', { 
template: '<h2>{{page}}</h2>', 
controller: ['$scope', function($scope){ 
$scope.page = 'home'; 
}] 
}) 
.when('/about', { 
template: '<h2>{{page}}</h2>', 
controller: ['$scope', function($scope){ 
$scope.page = 'about'; 
}] 
}) 
.otherwise({redirectTo: '/'}); 
}]); 
Plunker Example
Angular Dependency Injection and Services

Contenu connexe

Tendances (20)

Angular js
Angular jsAngular js
Angular js
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http client
 
The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Angular App Presentation
Angular App PresentationAngular App Presentation
Angular App Presentation
 
Angular 2: core concepts
Angular 2: core conceptsAngular 2: core concepts
Angular 2: core concepts
 
Angular2
Angular2Angular2
Angular2
 
Angular
AngularAngular
Angular
 
Angular 2 - a New Hope
Angular 2 - a New HopeAngular 2 - a New Hope
Angular 2 - a New Hope
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
 
Angular routing
Angular routingAngular routing
Angular routing
 
AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Introduction (Talk given on Aug 5 2013)AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Introduction (Talk given on Aug 5 2013)
 
Angular 5
Angular 5Angular 5
Angular 5
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questions
 
Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
 
AngularJS
AngularJSAngularJS
AngularJS
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
Angular 4 Introduction Tutorial
Angular 4 Introduction TutorialAngular 4 Introduction Tutorial
Angular 4 Introduction Tutorial
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 

En vedette

Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directivesAlexe Bogdan
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basicsRavindra K
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS introdizabl
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
 

En vedette (6)

Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
 
Ajs ppt
Ajs pptAjs ppt
Ajs ppt
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS intro
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 

Similaire à Angular Dependency Injection and Services

Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Frost
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar SimovićJS Belgrade
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs WorkshopRan Wahle
 
Angular Project Report
 Angular Project Report Angular Project Report
Angular Project ReportKodexhub
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JSBipin
 
Services Factory Provider Value Constant - AngularJS
Services Factory Provider Value Constant - AngularJSServices Factory Provider Value Constant - AngularJS
Services Factory Provider Value Constant - AngularJSSumanth krishna
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Katy Slemon
 
Angular presentation
Angular presentationAngular presentation
Angular presentationMatus Szabo
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIVisual Engineering
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular jsAayush Shrestha
 
Leveling up with AngularJS
Leveling up with AngularJSLeveling up with AngularJS
Leveling up with AngularJSAustin Condiff
 

Similaire à Angular Dependency Injection and Services (20)

17612235.ppt
17612235.ppt17612235.ppt
17612235.ppt
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Mean stack Magics
Mean stack MagicsMean stack Magics
Mean stack Magics
 
What is your money doing?
What is your money doing?What is your money doing?
What is your money doing?
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Angular Project Report
 Angular Project Report Angular Project Report
Angular Project Report
 
Angular In Depth
Angular In DepthAngular In Depth
Angular In Depth
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 
Services Factory Provider Value Constant - AngularJS
Services Factory Provider Value Constant - AngularJSServices Factory Provider Value Constant - AngularJS
Services Factory Provider Value Constant - AngularJS
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
Leveling up with AngularJS
Leveling up with AngularJSLeveling up with AngularJS
Leveling up with AngularJS
 
Angular2
Angular2Angular2
Angular2
 
Angular 2 in-1
Angular 2 in-1 Angular 2 in-1
Angular 2 in-1
 

Plus de Alexe Bogdan

Angular promises and http
Angular promises and httpAngular promises and http
Angular promises and httpAlexe Bogdan
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & AngularAlexe Bogdan
 
HTML & JavaScript Introduction
HTML & JavaScript IntroductionHTML & JavaScript Introduction
HTML & JavaScript IntroductionAlexe Bogdan
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communicationAlexe Bogdan
 
Angular Promises and Advanced Routing
Angular Promises and Advanced RoutingAngular Promises and Advanced Routing
Angular Promises and Advanced RoutingAlexe Bogdan
 
AngularJS - introduction & how it works?
AngularJS - introduction & how it works?AngularJS - introduction & how it works?
AngularJS - introduction & how it works?Alexe Bogdan
 

Plus de Alexe Bogdan (6)

Angular promises and http
Angular promises and httpAngular promises and http
Angular promises and http
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & Angular
 
HTML & JavaScript Introduction
HTML & JavaScript IntroductionHTML & JavaScript Introduction
HTML & JavaScript Introduction
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communication
 
Angular Promises and Advanced Routing
Angular Promises and Advanced RoutingAngular Promises and Advanced Routing
Angular Promises and Advanced Routing
 
AngularJS - introduction & how it works?
AngularJS - introduction & how it works?AngularJS - introduction & how it works?
AngularJS - introduction & how it works?
 

Dernier

PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationLinaWolf1
 
Intellectual property rightsand its types.pptx
Intellectual property rightsand its types.pptxIntellectual property rightsand its types.pptx
Intellectual property rightsand its types.pptxBipin Adhikari
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Paul Calvano
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Dana Luther
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一Fs
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一Fs
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一Fs
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhimiss dipika
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Sonam Pathan
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxeditsforyah
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...Excelmac1
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITMgdsc13
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012rehmti665
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一Fs
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一z xss
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 

Dernier (20)

PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 Documentation
 
Intellectual property rightsand its types.pptx
Intellectual property rightsand its types.pptxIntellectual property rightsand its types.pptx
Intellectual property rightsand its types.pptx
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhi
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptx
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITM
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 

Angular Dependency Injection and Services

  • 1.
  • 2.
  • 3.
  • 4.
  • 5. (DI) is a software design pattern that deals with how components get hold of their dependencies. The Angular injector subsystem is in charge of creating components, their , and them to other components as requested.
  • 6. Angular modules have the opportunity to configure themselves before the module actually bootstraps and starts to run. This phase is the only part of the Angular flow that can modify things before the app starts up. The only services that can be injected in this block are and ; Executed at begining of the application; Similar with the method in other programming languages; Any service can be injected here.
  • 7. singleton objects that are instantiated only once per application; lazy-loaded (created only when necessary); provide a way to share data and behavior across controllers, directives, filters or other services; How to create a service? Build your own DI system .constant(); .value(); .service(); .factory(); .provider();
  • 8. used for registering a constant service such as string, number, array, object or function; doesn't have the ability to use other services (have dependencies); angular .module('myApp', []) .constant('apiUrl', 'http://localhost:8080') .config(['apiUrl', function(apiUrl){ //apiUrl can be used here }]) .run(['$rootScope', function($rootScope){ //apiUrl can be used here }]);
  • 9. used for registering a value service such as string, number, array, object or function; doesn't have the ability to use other services (have dependencies); angular .module('myApp', []) .value('objectValue', { foo: 'bar' }) .run(['$rootScope', 'objectValue', function($rootScope, objectValue){ $rootScope.foo = objectValue.foo; } ]);
  • 10. used for registering a service factory which will be to return the ; ability to use other services (have dependencies); service initialization; delayed/lazy initialization. angular .module('myApp', []) .factory('myFactory', function(){ var data; //private variable return { fetchData: function(){ //business to populate data }, getData: function(){ return data; } } }) .run(['$rootScope', 'myFactory', function($rootScope, myFactory){ myFactory.fetchData(); $rootScope.data = myFactory.getData(); } ]);
  • 11. used for registering a service constructor which will be invoked with to create the ; same as factory; angular .module('myApp', []) .service('myService', function(){ var data; //private variable this.fetchData= function(){ //business to populate data }; this.getData= function(){ return data; }; }); .factory('myService', function(){ var Service = function(){ var data; //private variable this.fetchData= function(){ //business to populate data }; this.getData= function(){ return data; }; }; return new Service(); });
  • 12. , whose instance is responsible for ' ' a ; can have additional methods that allow configuration of the provider or it's returning service; must have a : that returns the factory service; ability to use other services (have dependencies);
  • 13. angular .module('myApp', []) .provider('myFactory', function(){ var configVar = 'value'; //The factory Service - can have any dependency this.$get = ['$http', function($http){ var data; //private variable return{ fetchData: function(){ //business to populate data }, getData: function(){ return data; } }; }]; //Config method this.config = function(config){ configVar = config; }; }) .config(['myFactoryProvider', function(myFactoryProvider){ myFactoryProvider.config('Overriden value'); }]) .run(['$rootScope', 'myFactory', function($rootScope, myFactory){ myFactory.fetchData(); $rootScope.data = myFactory.getData() } ]);
  • 14. Angular comes with several built-in services like: $http / $httpProvider; $compile / $compileProvider; many more. The is a convention to point that the service comes from the framework and it is not custom-made;
  • 15. All the providers (services) are . That means that they are all ; A constant is a value that can be injected everywhere. The value of a constant can never be changed; A value is just a simple injectable value; A service is an injectable constructor; A factory is an injectable function; A provider is a configurable factory.
  • 17.
  • 18. module is a core Angular module for basic routing. The module provides the directive, in order to render the appropriate template. Any time the route is changed the directive will update it's content: if there is a template associated with the current route: link the controller (if specified) with the scope; link the new scope with the new template; remove the last view and clean the last scope; create a new scope - inherited from the parent;
  • 19. To create routes on a specific module or app, exposes the . To add a specific route, has the method $routeProvider .when('path', { template: '', templateUrl: '', controller: '', controllerAs: '' //... }) .otherwise(routeConfigObj);
  • 20. <html ng-app="myApp"> <head>...</head> <body> <header> <h1>My app</h1> <ul> <li><a href="#/">Home</a></li> <li><a href="#/about">About</a></li> </ul> </header> <div class="content"> <div ng-view></div> </div> </body> </html> angular .module('myApp', ['ngRoute']) .config(['$routeProvider', function($routeProvider){ $routeProvider .when('/', { template: '<h2>{{page}}</h2>', controller: ['$scope', function($scope){ $scope.page = 'home'; }] }) .when('/about', { template: '<h2>{{page}}</h2>', controller: ['$scope', function($scope){ $scope.page = 'about'; }] }) .otherwise({redirectTo: '/'}); }]); Plunker Example