最近在接触
AngularJS
, 被晦涩难懂的各种概念搞得头疼脑涨。Service
、Factory
、Provider
就是其中几个。
Factory
Syntax: module.factory('factoryName', function)
Result: the value that is returned by invoking the function reference passed to module.factory
source code:
return factoryName = fn()
Note
- Must return an object which is the main difference differentiates it with other design patterns. We can expose any functionality to where it be injected.
Services
Syntax: module.service('serviceName', function)
Result: we will be provided with an instance of the function. In other words new FunctionYouPassedToService()
source code:
return serviceName = new fn()
Note:
Don’t need to return any object when create a AngularJS service for it can be instantiated by Angular self.
All we need to do is just add the functionality to
this
variable.
Provider
Syntax: module.provider('providerName', function)
Result: we will be provided with (new ProviderFunction()).$get()
source code:
return providerName = (new fn()).$get()
Note
1. Provider is one of which can be configured (another is constant
) during the module configuration phase
- Expose any more functionality through provider add it under
$get
like:1
2
3
4
5
6
7
8this.$get: function() {
return {
name: _name,
getName: function() {
return name
}
}
}
Additional
1 | anuglar.module('myModule', []) |
reference
AngularJS: Service vs Factory vs Providers made super simple – Ng-ninja
AngularJS: Service vs provider vs factory
– Stack Overflow
Additional
Constant
A constant can not be intercepted by a decorator, that means that the value of a constant should never be changed (though it is still possible to change it programmatically in Angular 1.x).
Value
A value is nothing more than a simple injectable value. The value can be a string, number but also a function. Value differs from constant in that value can not be injected into configurations, but it can be intercepted by decorators.
Decorator
A decorator can modify or encapsulate other providers. There is one exception and that a constant cannot be decorated.
##Summary
- All the providers are instantiated only once. That means that they are all singletons.
All the providers except constant can be decorated. - 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 decorator can modify or encapsulate other providers except a constant.
- A provider is a configurable factory.