繁体   English   中英

代理/装饰$ http.get

[英]Delegating/ Decorating $http.get in angular

下页有一个示例,用于在angular中委派$ log的调试功能。

http://solutionoptimist.com/2013/10/07/enhance-angularjs-logging-using-decorators/

同样,我想委托$ http服务的get / post函数。 这样我就可以记录我的应用程序执行的所有请求。

下面是我的代码

 $provide.decorator('$http', ["$delegate", function($delegate) {
  var debugFn = $delegate.get;
  $delegate.get = function() {
    var args = [].slice.call(arguments);

    // Prepend timestamp
    console.log(args[0]);

    // Call the original with the output prepended with formatted timestamp
    debugFn.apply(null, args)
  };

  return $delegate;
}]);

即使它正在记录网址,但之后仍会引发异常

TypeError: Cannot read property 'finally' of undefined
at handleRequestFn (angular.js:17382)
at compileTemplateUrl (angular.js:8270)
at applyDirectivesToNode (angular.js:7885)
at compileNodes (angular.js:7431)
at compile (angular.js:7338)
at applyDirectivesToNode (angular.js:7808)
at compileNodes (angular.js:7431)
at compileNodes (angular.js:7443)
at compile (angular.js:7338)
at angular.js:1630

我想念什么?

您需要返回原始get函数的结果(承诺):

$delegate.get = function() {
    var args = [].slice.call(arguments);

    // Prepend timestamp
    console.log(args[0]);

    // Call the original with the output prepended with formatted timestamp
    return debugFn.apply(null, args)
};

这样我就可以记录我的应用程序执行的所有请求。

您可以改用拦截器 (请参阅https://docs.angularjs.org/api/ng/service/$http


一个有用的示例链接-http: //www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM