AngularJS 中的非单例服务 [英] Non-Singleton Services in AngularJS

查看:25
本文介绍了AngularJS 中的非单例服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS 在其文档中明确声明服务是单例:

AngularJS clearly states in its documentation that Services are Singletons:

AngularJS services are singletons

与直觉相反,module.factory 也返回一个 Singleton 实例.

Counterintuitively, module.factory also returns a Singleton instance.

鉴于非单例服务有很多用例,实现工厂方法返回服务实例的最佳方法是什么,以便每次 ExampleService 依赖项是声明,它被 ExampleService 的不同实例满足?

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?

推荐答案

我认为我们不应该让工厂返回一个 newable 函数,因为这开始破坏依赖注入和库会表现得笨拙,尤其是对于第三方.简而言之,我不确定非单例服务是否有任何合法用例.

I don't think we should ever have a factory return a newable 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.

完成同样事情的更好方法是使用工厂作为 API 来返回一个对象集合,这些对象带有附加的 getter 和 setter 方法.下面是一些伪代码,展示了如何使用这种服务:

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();
  };
});

这只是用于通过 ID 查找小部件然后能够保存对记录所做的更改的伪代码.

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.

我现在没有时间,但如果有帮助,我可以稍后组装一个简单的Plunker来演示.

I don't have time right now, but if it will be helpful I can put together a simple Plunker later to demonstrate.

这篇关于AngularJS 中的非单例服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆