AngularJs loader while http request are ongoing on page -
i have developed application want add loader in system see find blockui , make 1 demo have kept in every pages.
so there option there in angular can include in 1 page , manage flag 1 place http calls?
and should remove after api calls done.
just create factory $http calls , in factory add listeners ajax load , end events. in listeners add loader show , hide events. here sample code. here have used google map service location , angular broadcast events show , hide loader.
<!doctype html> <html ng-app="plunker"> <head> <meta charset="utf-8"> <title>angularjs plunker</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script> var app = angular.module('plunker', []); app.config(function ($httpprovider) { }); app.factory('httppreconfig', ['$http', '$rootscope', function ($http, $rootscope) { $http.defaults.transformrequest.push(function () { console.log('started'); //show loader here $rootscope.$broadcast('ajaxprogress'); }); $http.defaults.transformresponse.push(function () { console.log('stopped'); //hide loader here $rootscope.$broadcast('ajaxfinished'); }) return $http; }]); app.controller('mainctrl', function ($scope, $rootscope, httppreconfig) { $scope.showloader = false; $scope.sendget = function () { httppreconfig.get('http://maps.googleapis.com/maps/api/geocode/json?address=america'); }; $rootscope.$on('ajaxprogress', function(){ $scope.showloader = true; }); $rootscope.$on('ajaxfinished', function(){ $scope.showloader = false; }); }); </script> </head> <body ng-controller="mainctrl"> <div ng-if="showloader">loader</div> <button ng-click="sendget()">send get</button> </body> </html> i hope helpful you.
Comments
Post a Comment