Show modal when device Network Status is offline


Hello Fellow Ionions,

I am new to angular and ionic and I have been enjoying myself learning this awesome framework and angular. I am currently building an app which relies heavily on $http (ajax) request.

I wanted to find out, what is the best practice to show a modal when the device goes offline. Built in Cordova plugin, ng-cordova or does ionic has something?

My questions are:

  • Is there a global event listener that I can use or register, that will fire an event and launch the modal whenever the device goes offline? If so, how? Should it be a service or within the main app controller?
  • When the device is back online, I want to be able to hide the modal. Can this be done?

Any help here would be appreciated.


Kind Regards,


Please take a look at the cordova events, they fire “online” and “offline” corrsepondingly. The big challenge is in supporting the same functionality for desktop browsers though…


For browsers, you can use navigator events , but they will not work properly on anything else other than Internet Explorer in my experience. So the best way to do it is check the connection with a 1x1 pixel image hosted somewhere, and using onload and onerror to see where it was loaded or not. Also you must use custom javascript random so that the file will not be cacheable. I do it like this:

    $rootScope.refreshConnectionState = function checkConnectionWithImage() {
        var connected = true;
        var imageid = document.getElementById("somerandomID");
        if (!(imageid))
        var img = document.createElement('img');"somerandomID";
        img.src = "" + Math.random();
        img.onerror = function () {
            if ($ == true)
            connected = false;
            $ = false;
        img.onload = function () {
            if ($ == false)
            connected = true;
            $ = true;

But make sure to use the refreshConnection() in your HTML templates, as ng-init , or ng-click, depending on how you need it.From there you can $rootScope.$on(‘becomeOnline’,showmodal());

For the modal part, here’s an example for how to use it. ,it’s pretty simple an straightforward . Here is more documentation$ionicModal/

I hope it helped you my friend,with alll this md5hash-majoogaly like a someboooooooooody