Search code examples
angularjscordovanetwork-connection

AngularJS, cordova, check connection on every request


I have an app that has many controllers and most of them are sending requests to the server. What I would like to do is to check if there is internet connection.

I do that using:

   if(navigator.network.connection.type == Connection.NONE){
        alert("Oops! You are not connected to the internet!");
    }else{
        alert("Yes! You are connected to the internet!");
  };

Where in app.js and how should I make it to check if there is internet connection on every request made? I tried to add to .config just before my authInterceptor but I can't make it work.

Ps. It works only if I place it in a function inside a controller and call it.


Solution

  • It sounds like you want to write an http interceptor. It's basically a special service you register that gets fired with every request. You can check if you have a connection in there and do what you will with that info. Here is a decent example of how to implement one

    http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/