Angular JS - service, factory & provider

Edit

service

在AngularJS中service是单例的,所以通常用service在controller之间,甚至模块之间来共享数据。

angular.module('app', [])
.controller('ctrl', Ctrl)
.service('CustomService', CustomService);
function CustomService(){
const service = this
service.a = "test"
}
Ctrl.$inject = ['CustomService']
function Ctrl(CustomService){
console.log(CustomService.a)
}

前面一个字符串是这个service的名字,后面一个是这个service的构造函数。可以如上在controller中使用。

factory

工厂函数,可以用来生成任意想要的东西,当其返回一个service实例时,和.service作用是相同的,如下

ShoppingListController2.$inject = ['ShoppingListFactory'];
function ShoppingListController2(ShoppingListFactory) {
var list2 = this;
// Use factory to create new shopping list service
var shoppingList = ShoppingListFactory(3);
...
}
function ShoppingListFactory() {
var factory = function (maxItems) {
return new ShoppingListService(maxItems);
};
return factory;
}

provider

factory其实是一种特殊的provider。看Angular JS的实现:

function factory(name, factoryFn, enforce) {
return provider(name, {
$get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn
});
}

factory函数都是用provider来实现的。
一个provider函数通常如下定义:

function ServiceProvider() {
var provider = this;
provider.config = {…};
provider.$get = function () {
var service =
new Service(provider.config.prop);
return service;
};
}

provider.$get来提供factory功能。它还能提供其他的配置项。然后provider还有一个好处是,在module.config函数中,只能inject provider。.config是在module定义以后,先于其他服务,立即运行的。如下:

angular.module('app', [])
.controller('ctrl', Ctrl)
.provider('Service', ServiceProvider)
.config(Config);
Config.$inject = ['ServiceProvider'];
function Config(ServiceProvider) {
ServiceProvider.config.prop = 'value';
};

这样的话,在$get实例化的service就能根据模块的不同来定制不同的功能。

%23%20Angular%20JS%20-%20service%2C%20factory%20%26%20provider%0A@%28myblog%29%5Bangular%5D%0A%0A%0A%23%23%20service%0A%u5728AngularJS%u4E2Dservice%u662F%u5355%u4F8B%u7684%uFF0C%u6240%u4EE5%u901A%u5E38%u7528service%u5728controller%u4E4B%u95F4%uFF0C%u751A%u81F3%u6A21%u5757%u4E4B%u95F4%u6765%u5171%u4EAB%u6570%u636E%u3002%0A%60%60%60%0Aangular.module%28%27app%27%2C%20%5B%5D%29%0A%20%20.controller%28%27ctrl%27%2C%20Ctrl%29%0A%20%20.service%28%27CustomService%27%2C%20CustomService%29%3B%0A%0Afunction%20CustomService%28%29%7B%0A%09const%20service%20%3D%20this%0A%09service.a%20%3D%20%22test%22%0A%7D%0A%0ACtrl.%24inject%20%3D%20%5B%27CustomService%27%5D%0Afunction%20Ctrl%28CustomService%29%7B%0A%09console.log%28CustomService.a%29%0A%7D%0A%60%60%60%0A%u524D%u9762%u4E00%u4E2A%u5B57%u7B26%u4E32%u662F%u8FD9%u4E2Aservice%u7684%u540D%u5B57%uFF0C%u540E%u9762%u4E00%u4E2A%u662F%u8FD9%u4E2Aservice%u7684%u6784%u9020%u51FD%u6570%u3002%u53EF%u4EE5%u5982%u4E0A%u5728controller%u4E2D%u4F7F%u7528%u3002%0A%0A%23%23%20factory%0A%u5DE5%u5382%u51FD%u6570%uFF0C%u53EF%u4EE5%u7528%u6765%u751F%u6210%u4EFB%u610F%u60F3%u8981%u7684%u4E1C%u897F%uFF0C%u5F53%u5176%u8FD4%u56DE%u4E00%u4E2Aservice%u5B9E%u4F8B%u65F6%uFF0C%u548C.service%u4F5C%u7528%u662F%u76F8%u540C%u7684%uFF0C%u5982%u4E0B%0A%60%60%60%0AShoppingListController2.%24inject%20%3D%20%5B%27ShoppingListFactory%27%5D%3B%0Afunction%20ShoppingListController2%28ShoppingListFactory%29%20%7B%0A%20%20var%20list2%20%3D%20this%3B%0A%0A%20%20//%20Use%20factory%20to%20create%20new%20shopping%20list%20service%0A%20%20var%20shoppingList%20%3D%20ShoppingListFactory%283%29%3B%0A%20%20...%0A%7D%0A%0Afunction%20ShoppingListFactory%28%29%20%7B%0A%20%20var%20factory%20%3D%20function%20%28maxItems%29%20%7B%0A%20%20%20%20return%20new%20ShoppingListService%28maxItems%29%3B%0A%20%20%7D%3B%0A%0A%20%20return%20factory%3B%0A%7D%0A%60%60%60%0A%0A%23%23%20provider%0Afactory%u5176%u5B9E%u662F%u4E00%u79CD%u7279%u6B8A%u7684provider%u3002%u770BAngular%20JS%u7684%u5B9E%u73B0%uFF1A%0A%60%60%60%0Afunction%20factory%28name%2C%20factoryFn%2C%20enforce%29%20%7B%0A%20%20return%20provider%28name%2C%20%7B%0A%20%20%20%20%24get%3A%20enforce%20%21%3D%3D%20false%20%3F%20enforceReturnValue%28name%2C%20factoryFn%29%20%3A%20factoryFn%0A%20%20%7D%29%3B%0A%7D%0A%60%60%60%0Afactory%u51FD%u6570%u90FD%u662F%u7528provider%u6765%u5B9E%u73B0%u7684%u3002%0A%u4E00%u4E2Aprovider%u51FD%u6570%u901A%u5E38%u5982%u4E0B%u5B9A%u4E49%uFF1A%0A%60%60%60%0Afunction%20ServiceProvider%28%29%20%7B%0A%20%20var%20provider%20%3D%20this%3B%0A%20%20provider.config%20%3D%20%7B%u2026%7D%3B%0A%20%20provider.%24get%20%3D%20function%20%28%29%20%7B%0A%20%20%20%20var%20service%20%3D%0A%20%20%20%20%20%20new%20Service%28provider.config.prop%29%3B%0A%20%20%20%20return%20service%3B%0A%20%20%7D%3B%0A%7D%0A%60%60%60%0Aprovider.%5C%24get%u6765%u63D0%u4F9Bfactory%u529F%u80FD%u3002%u5B83%u8FD8%u80FD%u63D0%u4F9B%u5176%u4ED6%u7684%u914D%u7F6E%u9879%u3002%u7136%u540Eprovider%u8FD8%u6709%u4E00%u4E2A%u597D%u5904%u662F%uFF0C%u5728module.config%u51FD%u6570%u4E2D%uFF0C%u53EA%u80FDinject%20provider%u3002.config%u662F%u5728module%u5B9A%u4E49%u4EE5%u540E%uFF0C%u5148%u4E8E%u5176%u4ED6%u670D%u52A1%uFF0C%u7ACB%u5373%u8FD0%u884C%u7684%u3002%u5982%u4E0B%uFF1A%0A%60%60%60javascript%0Aangular.module%28%27app%27%2C%20%5B%5D%29%0A%20%20.controller%28%27ctrl%27%2C%20Ctrl%29%0A%20%20.provider%28%27Service%27%2C%20ServiceProvider%29%0A%20%20.config%28Config%29%3B%0AConfig.%24inject%20%3D%20%5B%27ServiceProvider%27%5D%3B%0Afunction%20Config%28ServiceProvider%29%20%7B%0A%20%20ServiceProvider.config.prop%20%3D%20%27value%27%3B%0A%7D%3B%0A%60%60%60%0A%u8FD9%u6837%u7684%u8BDD%uFF0C%u5728%5C%24get%u5B9E%u4F8B%u5316%u7684service%u5C31%u80FD%u6839%u636E%u6A21%u5757%u7684%u4E0D%u540C%u6765%u5B9A%u5236%u4E0D%u540C%u7684%u529F%u80FD%u3002