Home  >  Q&A  >  body text

angular.js - angularjs中的拦截器会拦截哪些请求?

在angularjs中添加拦截器,发现$http发出的请求会拦截,但$window.location.href确不会拦截,想请问一下拦截器是不是只拦截$http发出的请求?

滿天的星座滿天的星座2734 days ago717

reply all(4)I'll reply

  • 为情所困

    为情所困2017-05-15 17:15:17

    The official document explains clearly and has examples
    https://docs.angularjs.org/ap...$http

    // register the interceptor as a service
    $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
      return {
        // optional method
        'request': function(config) {
          // do something on success
          return config;
        },
    
        // optional method
       'requestError': function(rejection) {
          // do something on error
          if (canRecover(rejection)) {
            return responseOrNewPromise
          }
          return $q.reject(rejection);
        },
    
    
    
        // optional method
        'response': function(response) {
          // do something on success
          return response;
        },
    
        // optional method
       'responseError': function(rejection) {
          // do something on error
          if (canRecover(rejection)) {
            return responseOrNewPromise
          }
          return $q.reject(rejection);
        }
      };
    });
    
    $httpProvider.interceptors.push('myHttpInterceptor');
    
    
    // alternatively, register the interceptor via an anonymous factory
    $httpProvider.interceptors.push(function($q, dependency1, dependency2) {
      return {
       'request': function(config) {
           // same as above
        },
    
        'response': function(response) {
           // same as above
        }
      };
    });

    reply
    0
  • 高洛峰

    高洛峰2017-05-15 17:15:17

    Jump to a new page without executing the code in the interceptor

    reply
    0
  • 怪我咯

    怪我咯2017-05-15 17:15:17

    I remember it was html and interface request, I console.log it before

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-15 17:15:17

    The so-called $window 其实是对浏览器 window secondary packaging of object references, so why is there this thing? The purpose is mainly for code testability.

    So, the conclusion is that this thing has nothing to do with $http, and naturally it will not use interceptors

    Of course, I still understand the title of the question, but I just hope to do some extra things when making jumps. This problem can only be solved from the perspective of routing.

    Above!

    reply
    0
  • Cancelreply