angular.js - angularjs中的拦截器会拦截哪些请求?
问题描述
在angularjs中添加拦截器,发现$http发出的请求会拦截,但$window.location.href确不会拦截,想请问一下拦截器是不是只拦截$http发出的请求?
问题解答
回答1:官方文档解释的比较清楚,也有示例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 } };});回答2:
跳转到新的页面不执行拦截器中的代码
回答3:我记得是html 与 接口请求,之前console.log过
回答4:所谓 $window 其实是对浏览器 window 对象的引用的二次包装,那为什么会有这个东东呢?目的主要是为了代码可测试性。
所以,结论是这玩意跟 $http 一点关系都没有,自然也不会走拦截器
当然,我还是挺懂题主,无非就是希望在做跳转时做一些额外的事情。这个问题,只能从路由方面去解决了。
以上!
相关文章:
1. javascript - 给js写的盒子添加css样式,css样式没起作用。2. 关于docker下的nginx压力测试3. 数据挖掘 - 如何用python实现《多社交网络的影响力最大化问题分析》中的算法?4. html - 开发android的调取我web页面 他之前用的是收费的浏览器包 显示是全的换了免费的浏览器包后 显示就不全 是什么原因啊5. 使用 CSS3 的 Media Query 浏览器会加载其他不同分辨率的 css 文件么6. 前端 - 类到底该如何去命名 .newsList 这种的命名难道真的不是过度语义化吗?~7. “Webdrivers”可执行文件可能具有错误的权限请参阅https://sites.google.com/a/chromium.org/chromedriver/home8. 登录密码匹配学号后六位数字9. 创建了这个目录还是会找不到这么决绝 thikphp510. 如何更新/删除指定的两条或多条数据

网公网安备