国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

angular.js - angularjs中的攔截器會攔截哪些請求?

瀏覽:91日期:2024-09-16 09:55:16

問題描述

在angularjs中添加攔截器,發現$http發出的請求會攔截,但$window.location.href確不會攔截,想請問一下攔截器是不是只攔截$http發出的請求?

問題解答

回答1:

官方文檔解釋的比較清楚,也有示例https://docs.angularjs.org/ap...$httpangular.js - angularjs中的攔截器會攔截哪些請求?

// 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 一點關系都沒有,自然也不會走攔截器

當然,我還是挺懂題主,無非就是希望在做跳轉時做一些額外的事情。這個問題,只能從路由方面去解決了。

以上!

相關文章:
主站蜘蛛池模板: 邵阳市| 郁南县| 顺义区| 兴文县| 海兴县| 冀州市| 双城市| 西乡县| 瑞金市| 连云港市| 象山县| 资溪县| 衡山县| 平阴县| 墨江| 连江县| 蒙山县| 灵丘县| 横山县| 阿巴嘎旗| 嫩江县| 淮滨县| 霞浦县| 芦山县| 永城市| 福清市| 江山市| 金湖县| 云和县| 元阳县| 盐亭县| 丹巴县| 全州县| 娄烦县| 江阴市| 水城县| 常宁市| 瑞丽市| 和政县| 大城县| 青海省|