angular.js - angularjs中的拦截器会拦截哪些请求?
滿天的星座
滿天的星座 2017-05-15 17:13:17
0
4
739

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

滿天的星座
滿天的星座

reply all(4)
为情所困

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
    }
  };
});
小葫芦

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

刘奇

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

曾经蜡笔没有小新

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!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template