<!-- 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;}]);然后通过它的名字添加到 $httpProvider.interceptors 数组:<!-- lang: js -->module.config(["$httpProvider", function($httpProvider) { $httpProvider.interceptors.push("myInterceptor");}]);先给大家展示下效果图:
本文通过对httpProvider注入拦截器实现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");}]);//loadingapp.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;}]);拦截器允许你: