博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angularjs Service vs provider vs factory
阅读量:6089 次
发布时间:2019-06-20

本文共 3160 字,大约阅读时间需要 10 分钟。

hot3.png

稍后翻译。

Services

Syntax: module.service( 'serviceName', function ); 

Result: When declaring serviceName as an injectable argument you will be provided with an instance of the function. In other words new FunctionYouPassedToService().

Factories

Syntax: module.factory( 'factoryName', function ); 

Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.

Providers

Syntax: module.provider( 'providerName', function ); 

Result: When declaring providerName as an injectable argument you will be provided with ProviderFunction().$get(). The constructor function is instantiated before the $get method is called - ProviderFunction is the function reference passed to module.provider.

Providers have the advantage that they can be configured during the module configuration phase.

See here for the provided code: 

Here's a great further explanation by Misko:

provide.value('a', 123);function Controller(a) {  expect(a).toEqual(123);}

In this case the injector simply returns the value as is. But what if you want to compute the value? Then use a factory

provide.factory('b', function(a) {  return a*2;});function Controller(b) {  expect(b).toEqual(246);}

So factory is a function which is responsible for creating the value. Notice that the factory function can ask for other dependencies.

But what if you want to be more OO and have a class called Greeter?

function Greeter(a) {  this.greet = function() {    return 'Hello ' + a;  }}

Then to instantiate you would have to write

provide.factory('greeter', function(a) {  return new Greeter(a);});

Then we could ask for 'greeter' in controller like this

function Controller(greeter) {  expect(greeter instanceof Greeter).toBe(true);  expect(greeter.greet()).toEqual('Hello 123');}

But that is way too wordy. A shorter way to write this would be provider.service('greeter', Greeter);

But what if we wanted to configure the Greeter class before the injection? Then we could write

provide.provider('greeter2', function() {  var salutation = 'Hello';  this.setSalutation = function(s) {    salutation = s;  }  function Greeter(a) {    this.greet = function() {      return salutation + ' ' + a;    }  }  this.$get = function(a) {    return new Greeter(a);  };});

We can then do this:

angular.module('abc', []).config(function(greeter2Provider) {  greeter2Provider.setSalutation('Halo');});function Controller(greeter2) {  expect(greeter2.greet()).toEqual('Halo 123');}

As a side note, servicefactory, and value are all derived from provider.

provider.service = function(name, Class) {  provider.provide(name, function() {    this.$get = function($injector) {      return $injector.instantiate(Class);    };  });}provider.factory = function(name, factory) {  provider.provide(name, function() {    this.$get = function($injector) {      return $injector.invoke(factory);    };  });}provider.value = function(name, value) {  provider.factory(name, function() {    return value;  });};

参考:http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory

转载于:https://my.oschina.net/u/214483/blog/475600

你可能感兴趣的文章
求有序数组中不重复数字的出现次数
查看>>
sql的where条件中包含中文,查询不出来的处理方法
查看>>
[笔记] 数码管显示(十一)
查看>>
github readme.md 添加图片
查看>>
喷水装置问题(一)
查看>>
理解镜像和容器,并运行whalesay镜像
查看>>
bzoj3504: [Cqoi2014]危桥
查看>>
淘宝购物车计算总价格
查看>>
面试之并发的解决方案
查看>>
springcloud之config 配置管理中心之配置属性加密解密
查看>>
mysql 单表,多表,符合条件,子查询
查看>>
web的越权访问的处理(步骤详解)
查看>>
2011,向前冲
查看>>
js拼接字符串后swiper不能动的解决方案
查看>>
微信支付接口开发(前言)
查看>>
Android 解析XML文件和生成XML文件
查看>>
windows 7下删除右键新建菜单项的多余选项
查看>>
知问前端——Ajax登录
查看>>
C++安装失败解决办法
查看>>
Object.defineProperty
查看>>