angularjs - 如何以 Angular 扩展 $http 服务?

标签 angularjs angular-http

不幸的是,我们一直在运行 1.2.26(当它被 gemified 时将升级到 1.2.28)。

同时,我怎样才能修补 (heh) $http 以便简写 patch方法可用吗?我对整个服务/工厂/模块的事情都很陌生。我已经进行了数小时的搜索,但似乎无法弄清楚。

myApp.factory('patchedHTTP', function($http, BasicService) {
  // $http['patch'] = function(url, data, config) {
  //   return $http(angular.extend(config || {}, {
  //     method: 'patch',
  //     url: url,
  //     data: data
  //   }));
  // };
  var extended = angular.extend(BasicService, {});
  extended.createShortMethodsWithData('patch');
  return extended;
});

以上是我所拥有的最好的......它没有做任何事情XD

最佳答案

你可以用一个 Angular 装饰器来做到这一点。

A service decorator intercepts the creation of a service, allowing it to override or modify the behaviour of the service. The object returned by the decorator may be the original service, or a new service object which replaces or wraps and delegates to the original service. For more information you can check angular documentation.



例子:
var app = angular.module('app');
app.decorator('$http', function ($delegate) {
  // NOTE: $delegate is the original service

  $delegate.patch = function () {
    // do the implementation here
  };

  return $delegate;
});

// usage
app.controller('SomeController', function($http) {
    $http.patch();
});

您可以保留此装饰器,直到您升级到某个较新的版本,而不仅仅是安全地删除它。

关于angularjs - 如何以 Angular 扩展 $http 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32853429/

相关文章:

angular - 有什么方法可以使用 RxJS 将 n 个 http 请求链接在一起吗?

javascript - 将 SocketIO 集成到用于 http 请求的 Angular 应用程序

javascript - angularjs ng-show 绑定(bind)不适用于 jQuery

javascript - Angular 工厂未从 Soundcloud (SC.get) 请求返回数据

angularjs - 将 ng-show 与 Controller 一起使用

javascript - AngularJS:需要子指令的父指令

javascript - 重新启动/重新加载 Angular 应用程序

Angular >= 4.3,httpClient.get 参数为空

c# - ASP.NET Core Angular 7 Http Post-需要复杂对象吗?

javascript - 如何从 $http post 对象返回值?