简体   繁体   English

对Angular js中的$ http拦截器感到困惑

[英]Confused about $http interceptors in Angular js

Really can not find good documentation about http interceptors in Angular js. 真的在Angular js中找不到有关http拦截器的好的文档。 While handling errors coused by ng-include i can intercept responseError by using this: 在处理ng-include错误时,我可以使用以下命令拦截responseError

    app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('templateInterceptor');
});

// register the interceptor as a service
app.factory('templateInterceptor', function($q) {
  return {
    'responseError': function(rejection) {
       var isTemplate = !!rejection.config.url.match(/^content/g);
       if (isTemplate) {
         // here we add error message, but how this message appesrs in the place of ng-include
         rejection.data = '<div><template-error url="\''+ (rejection.config.url) + '\'"><strong>Error from interceptor.</strong></template-error></div>';
         return rejection;
       } else {
         return $q.reject(rejection);
       }
    }
  }
});

This code was taken from this question how to catch angular ng-include error . 这段代码来自这个问题, 如何捕获角度ng-include错误 I don't get, how interceptors works? 我不明白,拦截器是如何工作的? What they must return? 他们必须返回什么? How to use rejection parameter that passed to the responseError interceptor? 如何使用传递给responseError拦截器的rejection参数? In rejection the data property is used to include error message into the place of failed ng-include directive, how this works? rejectiondata属性用于将错误消息包含在失败的ng-include指令中,这是如何工作的?

If you have call to $http , such as 如果您有呼叫$http ,例如

$http(....).then(function(results) {
  // Success callback
}, function(error) {
  // Error callback
});

And the server responds with an error, then the responseError interceptors will get invoked before running either the success or error callbacks. 服务器响应一个错误,然后在运行成功或错误回调之前将调用responseError拦截器。

  • If the final interceptor returns any value that is not a promise, then from the point of view of the calling code, the call to $http was successful, and the success callback will be executed, passing in the returned value as the results argument 如果最终的拦截器返回的不是Promise的任何值,则从调用代码的角度来看,对$http的调用已成功,并且将执行成功回调,并将返回的值作为results参数传递

  • If the final interceptor returns a promise that gets resolved with a value, then similar to the case above, from the point of view of the calling code, the call to $http was successful, and the success callback will be executed, passing in the resolved value as the results argument. 如果最终的拦截器返回的Promise值已被解析,则类似于上述情况,从调用代码的角度来看,对$http的调用已成功,并且将执行成功回调,并传递解析值作为results参数。

  • If the final interceptor returns a promise that is rejected with an error, then from the point of view of the calling code, the call to $http was not successful, and the error callback will be executed, passing in the rejected error as the error argument. 如果最终的拦截器返回的承诺被错误拒绝,则从调用代码的角度来看,对$http的调用成功,并且将执行错误回调,将被拒绝的错误作为error传递论点。

The ngInclude directive will inject into the page the data key from the results of a successful call to $http . ngInclude指令将从成功调用$http的结果中将data密钥注入到页面中。 The code at https://stackoverflow.com/a/20838764/1319998 transforms what would be an errored call to $http from ngInclude , into a success, with html of an error message as the data key in the result. https://stackoverflow.com/a/20838764/1319998上的代码将从ngInclude$http的错误调用转换为成功,并以错误消息的html作为结果中的data键。

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

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