角$ http服务单冲突冲突、http

2023-09-10 21:54:37 作者:笑着回忆你

$ http服务是单身,而我的$ P $与XHR / Ajax请求pvious经验证明有冲突,当两个请求共享同一个XHR对象。如果它不与角度有问题?如果是如何角手柄这样的情况?

$http service in angular is singleton, and my previous experience with xhr/ ajax request shown to have clash when two request share the same xhr object. Should it not a problem with angular ? if yes how angular handles such situation ?

推荐答案

我想你误解的事实是, $ HTTP 服务是一个单意味着所有要求以某种方式将共享相同的 XHR 对象。他们不知道。

I think you're misunderstanding the fact that the $http service is a singleton to mean that all requests somehow will share the same XHR object. They don't.

$ HTTP 服务本身是一个单,但是,这并不意味着请求共享相同的 XHR 对象。

The $http service itself is a singleton, but that doesn't mean that the requests share the same XHR object.

任何时候当你调用 $ HTTP 服务方法(例如, $ HTTP#GET ),它初始化新的异步请求。然而,它并没有初始化一个新的 $ HTTP 对象。

Anytime you call an $http service method (for example, $http#get), it initializes a new asynchronous request... However, it doesn't initialize a new $http object.

看看一些阿迪奥斯马尼的样品code为单例模式:

Take a look at some of Addy Osmani's sample code for the singleton pattern:

  return {
    getInstance: function () {

      if ( !instance ) {
        instance = init();
      }

      return instance;
    }
  };

单例模式简单地确保了 $ HTTP 服务本身不会被initiliazed一遍又一遍......但是,一个新的实例,它的没有按'牛逼的意思只有一个 XHR 对象。

The singleton pattern simply ensures that a new instance of the $http service itself doesn't get initiliazed over and over again... But it doesn't mean there is just one XHR object.

伪code为 $ HTTP 服务将是这个样子:

The pseudo-code for the $http service would look something like this:

var $httpProvider = (function() {
  var somePrivateConfig = "something important";

  var service = {
    request: function() {
      // make an HTTP request with an XHR object
    }
  };

  return {
    init: function() {
      // this is the important part that makes sure its a singleton
      window.$http = window.$http || service;
    }
  };

})();

/**
 * Something like this would be called whenever you
 * inject the $http service as a dependency... 
 * However, since it could be passed into multiple things all in the same runtime,
 * like controllers, directives, etc., we only need to initialize it ONE time
 */
$httpProvider.init();

/**
 * Now let's pretend we're inside, say, a controller.
 *
 * This method can safely be called many times,
 * but the method is always being called from the same singleton object
 */
$http.request();  

此外,你会发现有一个局部变量 somePrivateConfig $ httpProvider 的生活to之内。如果我们要重新初始化一个新的 $ HTTP 每一次它注入到组件(无论这是一个控制器指令,或其他),一个新的私有变量将被创建,但我们总是希望在整个 $ HTTP 对象的生命周期引用同一个值,这样我们就可以保证所有这些组件总是引用相同的信息。

Also, you'll notice that there's a local variable somePrivateConfig within the $httpProvider IIFE. If we were to re-initialize a new $http every time its injected to a component (whether that's a controller, directive, or whatever), a new private variable would be created, but we always want to be referencing that same value throughout the lifecycle of the $http object, so that we can guarantee that all those components are always referencing the same information.

不过这无关与 XHR 对象本身。我可能会滥用一些术语的上方和misre presented在哪里以及如何AngularJS的范围内提供者本身都被初始化为单一对象,但Singleton模式的原则仍然有效,因为它只是意味着异步请求包装类(这是 $ HTTP 服务)是一个单,但 XHR 不是

But this has nothing to do with the XHR object itself. I may have misused some of the terminology above and misrepresented where and how providers themselves within the context of AngularJS are initialized into singleton objects, but the principle of the singleton pattern still stands in that it simply means the async request wrapper "class" (which is the $http service) is a singleton, but the XHR isn't.