angularjs - ionic http request loading timeout -


i using following show loading screen whenever performing http request if there error stay loading (because of backdrop app becomes unusable). rather hide on every error checker wondering if possible call timeout after 5 seconds?

.config(function($httpprovider) {     $httpprovider.defaults.timeout = 5000;      $httpprovider.interceptors.push(function($rootscope) {         return {             request: function(config) {                 $rootscope.$broadcast('loading:show')                 return config             },             response: function(response) {                 $rootscope.$broadcast('loading:hide')                 return response             }         }     }) }) 

following jess's answer looks :

.config(function($httpprovider) {     $httpprovider.defaults.timeout = 5000;      $httpprovider.interceptors.push(function($rootscope) {         return {             request: function(config) {                 $rootscope.$broadcast('loading:show')                 return config             },             response: function(response) {                 $rootscope.$broadcast('loading:hide')                 return response             },             responseerror: function(response) {                 $rootscope.$broadcast('loading:hide')                 return response              },             requesterror: function(response) {                 $rootscope.$broadcast('loading:hide')                 return response             }         }     }) }) 

however cannot seem able put alert in requesterror inform user.

question how can implement alert notify user of error has occurred?

try adding responseerror , requesterror this:

 responseerror: function(responseerror) {                 $rootscope.$broadcast('loading:hide')                 return responseerror 

and again requesterror, angular http interceptors docs

requesterror: interceptor gets called when previous interceptor threw error or resolved rejection.

responseerror: interceptor gets called when previous interceptor threw error or resolved rejection.

edit answer comment:

so if want throw alert on responseerror add $rootscope.$broadcast('response:error')

in responseerror function

then in controller want throw alert in

$scope.$on('response:error', function(){throw error here}); 

you can same requesterror

this works because $broadcast -- dispatches event downwards child scopes


Comments