December 2014
Intermediate to advanced
346 pages
6h 14m
English
An often overlooked aspect of AngularJS services is their ability to decorate service types in the initialization logic. This allows you to add or modify how factories or services will behave in the config phase before they are injected in the application.
In the config phase, the $provide service offers a decorator method that allows you to inject a service and modify its definition before it is formally instantiated. This is shown here:
(app.js)
angular.module('myApp', [])
.config(function($provide) {
$provide.decorator('Player', function($delegate) {
// $delegate is the Player service instance
$delegate.setPlayer('Eli Manning');
return $delegate;
}); }) .controller('Ctrl', function($scope, Player) { $scope.data ...Read now
Unlock full access