主页 > 前端 > AngularJS >

Angularjs注入拦截器实现Loading效果_AngularJS_自学p

来源:自学PHP网    时间:2016-04-21 15:40 作者: 阅读:309次

[导读] angularjs作为一个全ajax的框架,对于请求,如果页面上不做任何操作的话,在结果反回来之前,页面是没有任何响应的,不像普通的HTTP请求,会有进度条之类...

angularjs作为一个全ajax的框架,对于请求,如果页面上不做任何操作的话,在结果烦回来之前,页面是没有任何响应的,不像普通的HTTP请求,会有进度条之类。

什么是拦截器?

$httpProvider 中有一个 interceptors 数组,而所谓拦截器只是一个简单的注册到了该数组中的常规服务工厂。下面的例子告诉你怎么创建一个拦截器:

  1. <!-- lang: js -->  
  2. module.factory('myInterceptor', ['$log'function($log) {  
  3.  $log.debug('$log is here to show you that this is a regular factory with injection');  
  4.  var myInterceptor = {  
  5.   ....  
  6.   ....  
  7.   ....  
  8.  };  
  9.  return myInterceptor;  
  10. }]);  

然后通过它的名字添加到 $httpProvider.interceptors 数组:

  1. <!-- lang: js -->  
  2. module.config(['$httpProvider'function($httpProvider) {  
  3.  $httpProvider.interceptors.push('myInterceptor');  
  4. }]);  

先给大家展示下效果图:

本文通过对httpProvider注入拦截器实现loading。

html代码

  1. <div class="loading-modal modal" ng-if="loading">  
  2.  <div class="loading">  
  3.   <img src="<?=$this->module->getAssetsUrl()?>/img/loading.gif" alt=""/><span ng-bind="loading_text"></span>  
  4.  </div>  
  5. </div>  

css代码

  1. .modal {  
  2.  position: fixed;  
  3.  width: 100%;  
  4.  height: 100%;  
  5.  left: 0;  
  6.  top: 0;  
  7.  z-index: 99;  
  8.  background: rgba(0, 0, 0, 0.3);  
  9.  overflow: hidden;  
  10. }  
  11. .loading {  
  12.  position: absolute;  
  13.  top: 50%;  
  14.  background: white;  
  15.  #solution> .border-radius(8px);  
  16.  width: 160px;  
  17.  height: 72px;  
  18.  left: 50%;  
  19.  margin-top: -36px;  
  20.  margin-left: -80px;  
  21.  text-align: center;  
  22.  img {  
  23.  margin-top: 12px;  
  24.  text-align: center;  
  25.  }  
  26.  span {  
  27.  display: block;  
  28.  }  
  29. }  

js代码

  1. app.config(["$routeProvider""$httpProvider"function ($routeProvider, $httpProvider) {  
  2.  $routeProvider.when('/', {  
  3.   templateUrl: "/views/reminder/index.html",  
  4.   controller: "IndexController"  
  5.  });  
  6.  $routeProvider.when('/create', {  
  7.   templateUrl: "/views/reminder/item/create.html",  
  8.   controller: "ItemCreateController"  
  9.  });  
  10.  $routeProvider.otherwise({redirectTo: '/'});  
  11.  $httpProvider.interceptors.push('timestampMarker');  
  12. }]);  
  13. //loading  
  14. app.factory('timestampMarker', ["$rootScope"function ($rootScope) {  
  15.  var timestampMarker = {  
  16.   request: function (config) {  
  17.    $rootScope.loading = true;  
  18.    config.requestTimestamp = new Date().getTime();  
  19.    return config;  
  20.   },  
  21.   response: function (response) {  
  22.    // $rootScope.loading = false;  
  23.    response.config.responseTimestamp = new Date().getTime();  
  24.    return response;  
  25.   }  
  26.  };  
  27.  return timestampMarker;  
  28. }]);  

拦截器允许你:

通过实现 request 方法拦截请求: 该方法会在 $http 发送请求道后台之前执行,因此你可以修改配置或做其他的操作。该方法接收请求配置对象(request configuration object)作为参数,然后必须返回配置对象或者 promise 。如果返回无效的配置对象或者 promise 则会被拒绝,导致 $http 调用失败。

通过实现 response 方法拦截响应: 该方法会在 $http 接收到从后台过来的响应之后执行,因此你可以修改响应或做其他操作。该方法接收响应对象(response object)作为参数,然后必须返回响应对象或者 promise。响应对象包括了请求配置(request configuration),头(headers),状态(status)和从后台过来的数据(data)。如果返回无效的响应对象或者 promise 会被拒绝,导致 $http 调用失败。

通过实现 requestError 方法拦截请求异常: 有时候一个请求发送失败或者被拦截器拒绝了。请求异常拦截器会俘获那些被上一个请求拦截器中断的请求。它可以用来恢复请求或者有时可以用来撤销请求之前所做的配置,比如说关闭进度条,激活按钮和输入框什么之类的。

通过实现 responseError 方法拦截响应异常: 有时候我们后台调用失败了。也有可能它被一个请求拦截器拒绝了,或者被上一个响应拦截器中断了。在这种情况下,响应异常拦截器可以帮助我们恢复后台调用。

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论