不幸的是,我们一直在运行 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/