Chapter 12
Providers
AngularJS offers several ways of registering providers (recipes for object instances).
Factories are probably the most common way of defining new singletons as follows:
angular.module('services.breadcrumbs', [])
.factory('breadcrumbs', ['$rootScope', '$location',
function($rootScope, $location){
...
}]);
The ability to define decorators around services allows us to easily enrich
existing code with additional functionality. The syntax to define decorators is
not very intuitive, especially when coupled with array-style DI annotations. To
make you more comfortable with this syntax, here is an example of a decorator
with DI annotations:
angular.module('services.exceptionHandler')
.config(['$provide', function($provide) {
$provide.decorator('$exceptionHandler', ['$delegate',
'exceptionHandlerFactory', function ($delegate,
exceptionHandlerFactory) {
return exceptionHandlerFactory($delegate);
}]);
}]);
Directives
Defining directives in a minification-safe way doesn't differ much from defining
other providers. For example, the field directive described in Chapter 9, Building
Advanced Directives would be defined as follows:
.directive('field', ['$compile', '$http', '$templateCache',
'$interpolate', function($compile, $http, $templateCache,
$interpolate) {
...
return {
restrict:'E',
priority: 100,
terminal: true
...
};
}]);