Home > Web Front-end > JS Tutorial > Angularjs injects interceptor to implement Loading effect_AngularJS

Angularjs injects interceptor to implement Loading effect_AngularJS

WBOY
Release: 2016-05-16 15:23:08
Original
1606 people have browsed it

Angularjs is a full Ajax framework. For requests, if no operations are performed on the page, there will be no response from the page until the results are returned. Unlike ordinary HTTP requests, there will be a progress bar and the like.

What is an interceptor?

There is an interceptors array in $httpProvider, and the so-called interceptor is just a regular service factory registered in the array. The following example shows you how to create an interceptor:

<!-- 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;
}]);
Copy after login

Then add it to the $httpProvider.interceptors array by its name:

<!-- lang: js -->
module.config(['$httpProvider', function($httpProvider) {
 $httpProvider.interceptors.push('myInterceptor');
}]);
Copy after login

Let me show you the renderings first:

This article implements loading by injecting interceptors into httpProvider.

html code

<div class="loading-modal modal" ng-if="loading">
 <div class="loading">
  <img src="<&#63;=$this->module->getAssetsUrl()&#63;>/img/loading.gif" alt=""/><span ng-bind="loading_text"></span>
 </div>
</div>
Copy after login

css code

.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;
 }
}
Copy after login

js code

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;
}]);
Copy after login

Interceptor allows you to:

Intercept requests by implementing the request method: This method will be executed before $http sends the request to the background, so you can modify the configuration or do other operations. This method receives a request configuration object as a parameter and must return a configuration object or a promise. If an invalid configuration object or promise is returned, it will be rejected, causing the $http call to fail.

Intercept the response by implementing the response method: This method will be executed after $http receives the response from the background, so you can modify the response or do other operations. This method receives a response object as a parameter and must return a response object or promise. The response object includes request configuration, headers, status and data from the background. If an invalid response object is returned or the promise will be rejected, the $http call will fail.

Intercept request exceptions by implementing the requestError method: Sometimes a request fails to be sent or is rejected by the interceptor. The request exception interceptor captures requests that were interrupted by the previous request interceptor. It can be used to restore the request or sometimes to undo the configuration made before the request, such as closing the progress bar, activating buttons and input boxes, etc.

Intercept response exceptions by implementing the responseError method: Sometimes our background call fails. It's also possible that it was rejected by a request interceptor, or interrupted by a previous response interceptor. In this case, the response exception interceptor can help us resume the background call.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template