https://github.com/Transcordia/ng-meetup
Transcordia LLC
Implement the Singleton pattern (instantiated by the $injector)
They are lazy-loaded
Bundle together similar functionality
Maintain state for the lifetime of the app
Provide cross-controller communication
Integrate with external dependencies
HTTP Requests ($http, $resources)
DOM Utilities ($filter, $document, $animate, $location)
Angular Management ($rootScope, $parse, $compile)
Helpers ($cacheFactory, $log, $timeout, $q)
Why use:
module.constant('apikey', 'E830AF11');
Software design pattern (Inversion of Control)
Determines how components get hold of their dependencies
Register a service
Provide a unique name
function myFilter( $locale ) {
var formats = $locale.NUMBER_FORMATS;
return something;
}
// Option 1
myFilter.$inject = ['$locale'];
$injector.invoke( myFilter );
// Option 2
$injector.invoke( ['$locale', myFilter] );Value
Constant
Factory
Service
Suitable for configuration data
Can be injected into module.config()
Value is a primitive or an object
Value can be a primitive, but is usually a function or object
If a function, not instantiated or invoked; simply returned
Cannot be injected into module.config()
Do not use for configuration data
Registers a factory function as a service
Can function similar to a value service
Registers a constructor function as a service
Initialized by AngularJS
Value, Factory and Service are all created using Provider()
Use Provider function directly when your service can be configured
Provide a $get() implementation
function value(name, val) {
return provider(name, {
$get: function() { return val; }
});
}
function factory(name, factoryFn) {
return provider(name, {
$get: factoryFn
});
}
function service(name, constructor) {
return provider(name, {
$get: function($injector) {
return $injector.instantiate(constructor);
}
});
}Dependency injection interceptors
Intercept providers
https://github.com/Transcordia/ng-meetup