委托/装饰角度为$ http.get

Sre*_*akh 0 decorator angularjs

下面的页面有一个示例,用于委托$ log in angular的调试功能.

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;
}]);
Run Code Online (Sandbox Code Playgroud)

即使它正在记录url,它也会抛出异常

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
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

ela*_*con 5

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

$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)
};
Run Code Online (Sandbox Code Playgroud)