The document discusses dependency injection (DI) in Angular and how it is used to configure services. It provides examples of different service types - constant, value, factory, service, and provider - and how they are registered and injected. The ngRoute module is also summarized as the core routing module in Angular that uses directives to render templates based on routes.
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.
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);