Angularjs注入攔截器實(shí)現(xiàn)Loading效果
angularjs作為一個(gè)全ajax的框架,對(duì)于請(qǐng)求,如果頁(yè)面上不做任何操作的話,在結(jié)果煩回來(lái)之前,頁(yè)面是沒(méi)有任何響應(yīng)的,不像普通的HTTP請(qǐng)求,會(huì)有進(jìn)度條之類(lèi)。
什么是攔截器?
$httpProvider 中有一個(gè) interceptors 數(shù)組,而所謂攔截器只是一個(gè)簡(jiǎn)單的注冊(cè)到了該數(shù)組中的常規(guī)服務(wù)工廠。下面的例子告訴你怎么創(chuàng)建一個(gè)攔截器:
<!-- lang: js --> module.factory('myInterceptor', ['$log', function($log) { $log.debug('$log is here to show you that this is a regular factory with injection'); var myInterceptor = { .... .... .... }; return myInterceptor; }]);
然后通過(guò)它的名字添加到 $httpProvider.interceptors 數(shù)組:
<!-- lang: js --> module.config(['$httpProvider', function($httpProvider) { $httpProvider.interceptors.push('myInterceptor'); }]);
先給大家展示下效果圖:
本文通過(guò)對(duì)httpProvider注入攔截器實(shí)現(xiàn)loading。
html代碼
<div class="loading-modal modal" ng-if="loading"> <div class="loading"> <img src="<?=$this->module->getAssetsUrl()?>/img/loading.gif" alt=""/><span ng-bind="loading_text"></span> </div> </div>
css代碼
.modal { position: fixed; width: 100%; height: 100%; left: 0; top: 0; z-index: 99; background: rgba(0, 0, 0, 0.3); overflow: hidden; } .loading { position: absolute; top: 50%; background: white; #solution> .border-radius(8px); width: 160px; height: 72px; left: 50%; margin-top: -36px; margin-left: -80px; text-align: center; img { margin-top: 12px; text-align: center; } span { display: block; } }
js代碼
app.config(["$routeProvider", "$httpProvider", function ($routeProvider, $httpProvider) { $routeProvider.when('/', { templateUrl: "/views/reminder/index.html", controller: "IndexController" }); $routeProvider.when('/create', { templateUrl: "/views/reminder/item/create.html", controller: "ItemCreateController" }); $routeProvider.otherwise({redirectTo: '/'}); $httpProvider.interceptors.push('timestampMarker'); }]); //loading app.factory('timestampMarker', ["$rootScope", function ($rootScope) { var timestampMarker = { request: function (config) { $rootScope.loading = true; config.requestTimestamp = new Date().getTime(); return config; }, response: function (response) { // $rootScope.loading = false; response.config.responseTimestamp = new Date().getTime(); return response; } }; return timestampMarker; }]);
攔截器允許你:
通過(guò)實(shí)現(xiàn) request 方法攔截請(qǐng)求: 該方法會(huì)在 $http 發(fā)送請(qǐng)求道后臺(tái)之前執(zhí)行,因此你可以修改配置或做其他的操作。該方法接收請(qǐng)求配置對(duì)象(request configuration object)作為參數(shù),然后必須返回配置對(duì)象或者 promise 。如果返回?zé)o效的配置對(duì)象或者 promise 則會(huì)被拒絕,導(dǎo)致 $http 調(diào)用失敗。
通過(guò)實(shí)現(xiàn) response 方法攔截響應(yīng): 該方法會(huì)在 $http 接收到從后臺(tái)過(guò)來(lái)的響應(yīng)之后執(zhí)行,因此你可以修改響應(yīng)或做其他操作。該方法接收響應(yīng)對(duì)象(response object)作為參數(shù),然后必須返回響應(yīng)對(duì)象或者 promise。響應(yīng)對(duì)象包括了請(qǐng)求配置(request configuration),頭(headers),狀態(tài)(status)和從后臺(tái)過(guò)來(lái)的數(shù)據(jù)(data)。如果返回?zé)o效的響應(yīng)對(duì)象或者 promise 會(huì)被拒絕,導(dǎo)致 $http 調(diào)用失敗。
通過(guò)實(shí)現(xiàn) requestError 方法攔截請(qǐng)求異常: 有時(shí)候一個(gè)請(qǐng)求發(fā)送失敗或者被攔截器拒絕了。請(qǐng)求異常攔截器會(huì)俘獲那些被上一個(gè)請(qǐng)求攔截器中斷的請(qǐng)求。它可以用來(lái)恢復(fù)請(qǐng)求或者有時(shí)可以用來(lái)撤銷(xiāo)請(qǐng)求之前所做的配置,比如說(shuō)關(guān)閉進(jìn)度條,激活按鈕和輸入框什么之類(lèi)的。
通過(guò)實(shí)現(xiàn) responseError 方法攔截響應(yīng)異常: 有時(shí)候我們后臺(tái)調(diào)用失敗了。也有可能它被一個(gè)請(qǐng)求攔截器拒絕了,或者被上一個(gè)響應(yīng)攔截器中斷了。在這種情況下,響應(yīng)異常攔截器可以幫助我們恢復(fù)后臺(tái)調(diào)用。
- AngularJS實(shí)現(xiàn)進(jìn)度條功能示例
- Spring Boot+AngularJS+BootStrap實(shí)現(xiàn)進(jìn)度條示例代碼
- 用AngularJS的指令實(shí)現(xiàn)tabs切換效果
- angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動(dòng)效果
- AngularJS實(shí)現(xiàn)按鈕提示與點(diǎn)擊變色效果
- AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫(huà)效果的方式總結(jié)
- 基于angularjs實(shí)現(xiàn)圖片放大鏡效果
- angularjs實(shí)現(xiàn)首頁(yè)輪播圖效果
- Angular實(shí)現(xiàn)的進(jìn)度條功能示例
相關(guān)文章
AngularJS 模塊詳解及簡(jiǎn)單實(shí)例
本文主要介紹AngularJS 模塊,這里幫大家整理了相關(guān)資料,詳細(xì)介紹了AngularJS的基礎(chǔ)知識(shí),有需要的朋友可以參考下2016-07-07AngularJS學(xué)習(xí)筆記之TodoMVC的分析
這篇文章主要介紹了AngularJS學(xué)習(xí)筆記之TodoMVC的分析的相關(guān)資料,需要的朋友可以參考下2015-02-02Angular6 發(fā)送手機(jī)驗(yàn)證碼按鈕倒計(jì)時(shí)效果實(shí)現(xiàn)方法
這篇文章主要介紹了Angular6 發(fā)送手機(jī)驗(yàn)證碼按鈕倒計(jì)時(shí)效果實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Angular請(qǐng)求防抖處理第一次請(qǐng)求失效問(wèn)題
這篇文章主要介紹了angular請(qǐng)求防抖及處理第一次請(qǐng)求失效的相關(guān)資料,需要的朋友可以參考下2019-05-05AngularJS 雙向數(shù)據(jù)綁定詳解簡(jiǎn)單實(shí)例
這篇文章主要介紹了AngularJS 雙向數(shù)據(jù)綁定詳解簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10詳細(xì)AngularJs4的圖片剪裁組件的實(shí)例
本篇文章主要介紹了詳細(xì)AngularJs4的圖片剪裁組件的實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07angularjs $http調(diào)用接口的方式詳解
今天小編就為大家分享一篇angularjs $http調(diào)用接口的方式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Angular入口組件(entry component)與聲明式組件的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04