I want to display the status of the warning message if I lose my internet connection

Using AngularJS, how can I display the status of the warning message if I lose the Internet connection in my application?

I am working on the Angularjs Express platform and API. while working on the application, if I lose my Internet connection, I need to issue a warning and I must stop causing HTTP requests until I get a proper Internet connection and resume work.

I even tried with offline.js could not solve, please, if some simple and easy way, let me know thanks. In the header, I added all of these

<script src="/javascripts/plugins/offlinechecking/offline.min.js"></script>
<link rel="stylesheet" href="/stylesheets/themes/offline-theme-chrome.css" />
<link rel="stylesheet" href="/stylesheets/themes/offline-language-english.css" />
<script>
  Offline.options = {
    checkOnLoad: false,
    interceptRequests: true,
    reconnect: {
      initialDelay: 3,
      delay: (1.5 * last delay, capped at 1 hour)
    },
    requests: true,
    game: false
</script>
+4
source share
2 answers

- -:

app.run(function($window, $rootScope) {
      $rootScope.online = navigator.onLine;
      $window.addEventListener("offline", function () {
        $rootScope.$apply(function() {
          $rootScope.online = false;
        });
      }, false);
      $window.addEventListener("online", function () {
        $rootScope.$apply(function() {
          $rootScope.online = true;
        });
      }, false);
});

: AngularJs

, Offline.options:

Offline.options = {
// No closing }.
+2

:

  • addEventListener , .body.

  • .ononline .onoffline
    document.body JavaScript.

  • ononline = "..." onoffline = "..."
      HTML

.

document.body.onoffline = function() {
   alert('You are offline now');
   $scope.connection = 'offline' 
}

document.body.ononline = function() {
   alert('You are online again');
   $scope.connection = 'online' 
}

$scope.connection, .


1

, factory

myApp.factory('connectionStatus', function(){
   var connection;

   return connection;
})

factory, . , .

myApp.controller('mainController', function($scope, connectionStatus) {
    document.body.onoffline = function() {
           alert('You are offline now');
           connectionStatus = 'offline' 
    }
})
0

Source: https://habr.com/ru/post/1623915/


All Articles