javascript 如果应用程序处于离线状态,我如何使用 AngularJS 进行检测?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15574580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:14:15  来源:igfitidea点击:

How can I detect with AngularJS if the app is offline?

javascriptangularjs

提问by Sebastien Rothlisberger

We have many customers in remote areas and lost of connectivity is frequent. How can I detect if the javascript app is offline? Meaning cannot reach the server and load the templates.

我们在偏远地区有很多客户,经常失去连接。如何检测javascript应用程序是否离线?意思是无法访问服务器并加载模板。

回答by Sebastien Rothlisberger

I finally found a JSFiddle that does exactly that: Check if the web application is online. It uses window.navigator.onLine.

我终于找到了一个可以做到这一点的 JSFiddle:检查 Web 应用程序是否在线。它使用window.navigator.onLine.

JSFiddle: http://jsfiddle.net/rommsen/QY8w2/

JSFiddle:http: //jsfiddle.net/rommsen/QY8w2/

Source: https://groups.google.com/d/msg/angular/ZncvSVUc9y4/S4jH1e_XgGoJ

资料来源:https: //groups.google.com/d/msg/angular/ZncvSVUc9y4/S4jH1e_XgGoJ

回答by justnajm

$scope.online = $window.navigator.onLine;

$window.addEventListener("offline", function() {
    $scope.online = false;
});

$window.addEventListener("online", function() {
    $scope.online = true;
});