angular.js - angularjs中的攔截器會(huì)攔截哪些請(qǐng)求?
問題描述
在angularjs中添加攔截器,發(fā)現(xiàn)$http發(fā)出的請(qǐng)求會(huì)攔截,但$window.location.href確不會(huì)攔截,想請(qǐng)問一下攔截器是不是只攔截$http發(fā)出的請(qǐng)求?
問題解答
回答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:
跳轉(zhuǎn)到新的頁面不執(zhí)行攔截器中的代碼
回答3:我記得是html 與 接口請(qǐng)求,之前console.log過
回答4:所謂 $window 其實(shí)是對(duì)瀏覽器 window 對(duì)象的引用的二次包裝,那為什么會(huì)有這個(gè)東東呢?目的主要是為了代碼可測(cè)試性。
所以,結(jié)論是這玩意跟 $http 一點(diǎn)關(guān)系都沒有,自然也不會(huì)走攔截器
當(dāng)然,我還是挺懂題主,無非就是希望在做跳轉(zhuǎn)時(shí)做一些額外的事情。這個(gè)問題,只能從路由方面去解決了。
以上!
相關(guān)文章:
1. 數(shù)組按鍵值封裝!2. docker不顯示端口映射呢?3. java - 阿里的開發(fā)手冊(cè)中為什么禁用map來作為查詢的接受類?4. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題5. python - flask _sqlalchemy 能否用中文作為索引條件6. python3.x - git bash如何運(yùn)行.bat文件?7. 跟蹤器怎么開啟無反應(yīng)8. 主題切換問題,用過別人的webapp在后臺(tái)切換模板主題后手機(jī)端打開網(wǎng)頁就是切換到的主題了9. clone - git sourceTree克隆倉庫時(shí),都不停彈出Password Required彈窗,即時(shí)輸入正確的git賬號(hào)密碼還是彈出10. html5 - 使用echarts中的圖表 一個(gè)頁面導(dǎo)入了好幾個(gè)js圖表 實(shí)現(xiàn)echarts圖表隨著瀏覽器窗口變化而變化時(shí)出現(xiàn)了問題
