Angularjs as a full AJAX framework, for the request, if the page does not do anything, before the results annoying back, the page is not any response, unlike normal HTTP requests, there will be a progress bar and so on.
What is an interceptor?
There is a interceptors array in the $httpProvider, and the so-called interceptor is simply a generic service factory registered to the array. The following example shows you how to create an interceptor:
<!--lang:js--module.factory (function($log) {$log. Debug (' $log is here-show you th At the regular factory with injection 'var myinterceptor = { .... .... return Myinterceptor;}]);
It is then added to the $httpProvider. Interceptors array by its name:
<!--lang:js-Module.config ([function($httpProvider) {$ HttpProvider.interceptors.push (' myinterceptor ');}]);
Let's show you the following:
In this paper, the loading is implemented by Httpprovider injection interceptor.
<Divclass= "Loading-modal modal"ng-if= "Loading"> <Divclass= "Loading"> <imgsrc= "<?= $this->module->getassetsurl ()? >/img/loading.gif"alt=""/><spanNg-bind= "Loading_text"></span> </Div></Div>
. modal {position:fixed; width:100%; height:100%; left:0; top:0; z-index:99; Background:rgba (0, 0, 0, 0.3); Overflo W: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;}}
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) {varTimestampmarker ={request:function(config) {$rootScope. Loading=true; Config.requesttimestamp=NewDate (). GetTime (); returnconfig; }, Response:function(response) {//$rootScope. Loading = false;Response.config.responseTimestamp =NewDate (). GetTime (); returnresponse; } }; returnTimestampmarker;}]);
Interceptors allow you to:
Intercepting requests by implementing the request method: The method executes before the $http sends the request to the background, so you can modify the configuration or do other things. The method receives the request configuration object (requests config objects) as a parameter, and then must return the configuration object or promise. If an invalid configuration object is returned or promise is rejected, the $http call fails.
Intercept a response by implementing the Response method: The method executes after the $http receives a response from the background, so you can modify the response or do something else. The method receives the Response object (response object) as a parameter, and then must return the response object or promise. The response object includes the request configuration, the header (headers), the State (status), and the data from the background. If an invalid response object is returned or promise is rejected, the $http call fails.
Intercept request exceptions by implementing the Requesterror method: Sometimes a request fails to send or is rejected by the interceptor. Request exception interceptors capture requests that were interrupted by a previous request interceptor. It can be used to restore the request or sometimes to undo the configuration that was made before the request, such as closing the progress bar, activating the button and the input box, and so on.
To intercept a response exception by implementing the Responseerror method: sometimes our back-end calls fail. It is also possible that it was rejected by a request interceptor or was interrupted by a response interceptor. In this case, the response exception blocker can help us to restore the background call.
Angularjs inject interceptors for loading effects