AngularJS:服务提供者VS VS厂 [英] AngularJS: Service vs provider vs factory
问题描述
什么是AngularJS之间的区别模块的服务
,提供
和工厂
?
What are the differences between AngularJS module's Service
, Provider
and Factory
?
推荐答案
从AngularJS邮件列表我的一个惊人的线索解释服务VS工厂VS提供商和他们注射使用。编译答案:
From the AngularJS mailing list I got an amazing thread that explains service vs factory vs provider and their injection usage. Compiling the answers:
语法: module.service(服务名,功能);
结果:当声明服务名作为参数注射您将提供功能的一个实例。换句话说 新FunctionYouPassedToService()
。
语法: module.factory('factoryName',功能);
结果:当声明factoryName,你将提供一种可注射的参数由调用传递函数引用的返回值来module.factory
语法: module.provider('的providerName',功能);
结果:当声明的providerName的注射参数您将提供 (新ProviderFunction())$得到()
。被称为$ get方法之前,构造函数被实例化 - ProviderFunction
是传递给module.provider函数引用
Syntax: module.provider( 'providerName', function );
Result: When declaring providerName as an injectable argument you will be provided with (new 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.
请参阅这里提供的code。
See here for the provided code.
下面是一个伟大的进一步解释由MISKO:
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.
但是,如果你想更OO,有一个叫Greeter类?
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');
}
但是,这是太罗嗦了。写这将是 provider.service('迎宾',招待员)较短的方式;
但是,如果我们想要在注射前配置迎宾
类?然后,我们可以写
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);
};
});
然后我们可以做到这一点:
Then we can do this:
angular.module('abc', []).config(function(greeter2Provider) {
greeter2Provider.setSalutation('Halo');
});
function Controller(greeter2) {
expect(greeter2.greet()).toEqual('Halo 123');
}
作为一个方面说明,服务
,工厂
和值
都是从商的。
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;
});
};
这篇关于AngularJS:服务提供者VS VS厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!