An idea about checking internet connection


#1

Hi. I develop an ionic application. I make service calls a lot of time and navigate between pages. In lot of projects, if there is no internet connection, it gives an warning that “No connection”. I control this event all button click events. How do you make this approach? I try to find a basic and general solution. Thanks in advance.


#2

Make a factory and use it in all your service requests instead of calling $http… everytime.

.controller('ctrlA', function($scope, ajaxService){
  $scope.callA = function(){
   ajaxService.get('someMethodA', { a: '' });
  }
})    
.controller('ctrlB', function($scope, ajaxService){
  $scope.callB = function(){
   ajaxService.get('someMethodB', { b: '' });
  }
})   
.factory("ajaxService", function($http){
    var api = "api.some.com";
      return {
       get: function(method,data){
        if(internet){
          return $http.get(api+method, data);
        }   
        else{
         // show alert that internet is working
       }
    }
     }
    })