问题描述
角清楚的说明文件中指出,服务是单身:
角服务是单身
直觉相反, module.factory
也返回一个Singleton实例。
由于有大量的应用案例非单服务,什么是实现工厂方法返回一个服务的情况下,最好的方式,让每一次的 ExampleService
依赖声明,它是由 ExampleService
?
我不认为我们应该永远有一个工厂返回一个新
能功能,因为这开始打破依赖注入和图书馆的行为笨拙,特别是对第三方。总之,我不知道有任何合法的用例非单服务队。
有一个更好的方式来完成同样的事情是用工厂作为API返回对象的集合与连接到他们的getter和setter方法。这里是显示使用这种服务的可能是如何工作的一些伪code:
.controller('MainCtrl',函数($范围,widgetService){
$ scope.onSearchFormSubmission =功能(){
widgetService.findById($ scope.searchById)。然后(功能(部件){
//这是一个返回的对象,完成了所有的getter / setter方法
$ scope.widget =部件;
});
}; $ scope.onWidgetSave =功能(){
//这个方法仍然存在widget对象
。$ scope.widget $保存();
};
});
这仅仅是伪code用于查找由ID的小部件,然后能够保存到记录所做的更改。
下面是一些伪code的服务:
.factory('widgetService',函数($ HTTP){ 功能的Widget(JSON){
angular.extend(这一点,JSON);
} Widget.prototype = {
$保存:功能(){
// TODO:带不相干的领域
VAR scrubbedObject = // ...
返回$ http.put('/widgets/'+this.id,scrubbedObject);
}
}; 功能getWidgetById(ID){
返回HTTP $('/部件/'+ ID)。然后(函数(JSON){
返回新控件(JSON);
});
}
//公共控件API
返回{
// ...
findById:getWidgetById
// ...
};
});
虽然在这个例子中不包括在内,这类灵活的服务还可以轻松管理状态。
我没有时间,现在,但是,如果这将有助于我以后可以组成一个简单的Plunker证明。
Angular clearly states in its documentation that Services are Singletons:
Angular services are singletons
Counterintuitively, module.factory
also returns a Singleton instance.
Given that there are plenty of use-cases for non-singleton services, what is the best way to implement the factory method to return instances of a Service, so that each time an ExampleService
dependency is declared, it is satisfied by a different instance of ExampleService
?
I don't think we should ever have a factory return a new
able function as this begins to break down dependency injection and the library will behave awkwardly, especially for third parties. In short, I am not sure there are any legitimate use cases for non-singleton sevices.
A better way to accomplish the same thing is to use the factory as an API to return a collection of objects with getter and setter methods attached to them. Here is some pseudo-code showing how using that kind of service might work:
.controller( 'MainCtrl', function ( $scope, widgetService ) {
$scope.onSearchFormSubmission = function () {
widgetService.findById( $scope.searchById ).then(function ( widget ) {
// this is a returned object, complete with all the getter/setters
$scope.widget = widget;
});
};
$scope.onWidgetSave = function () {
// this method persists the widget object
$scope.widget.$save();
};
});
This is just pseudo-code for looking up a widget by ID and then being able to save changes made to the record.
Here's some pseudo-code for the service:
.factory( 'widgetService', function ( $http ) {
function Widget( json ) {
angular.extend( this, json );
}
Widget.prototype = {
$save: function () {
// TODO: strip irrelevant fields
var scrubbedObject = //...
return $http.put( '/widgets/'+this.id, scrubbedObject );
}
};
function getWidgetById ( id ) {
return $http( '/widgets/'+id ).then(function ( json ) {
return new Widget( json );
});
}
// the public widget API
return {
// ...
findById: getWidgetById
// ...
};
});
Though not included in this example, these kinds of flexible services could also easily manage state.
I don't have time right now, but if it will be helpful I can put together a simple Plunker later to demonstrate.
这篇关于非单服务在角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!