How check network connection on every request?


#1

Hi all,

I have use $cordovaNetwork to check network connection when run app (only start app).

How i can check network connection on every request?

I am doing by bad way(check network state on every request):

if($cordovaNetwork.isOnline()){
//request
}
else{
//mess
}

Thank you!


#2

create a $http interceptor to check for network state before the request is made. https://docs.angularjs.org/api/ng/service/$http


#3

you can create a $http intercepter like aaronlsaunders said .

Intercept a request by implementing the request function: This method is called before $http
sends the request to the backend, so you can modify the configurations
and make other actions. This function receives the request configuration
object as a parameter and has to return a configuration object or a
promise. Returning an invalid configuration object or promise that will
be rejected, will make the $http call to fail.

here you find better example then the one in the docs http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/


#4

@aaronlsaunders @micha_r thank you so much! :smile:


#5

no problem… looks like you got what you needed


#6

Hi guys,
Can you please attach your code including the intercepter with the connection checking ($cordovaNetwork.isOnline()) ?
I tried to do the same in my project.

Thanks