简体   繁体   English

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

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

We have many customers in remote areas and lost of connectivity is frequent.我们在偏远地区有很多客户,经常失去连接。 How can I detect if the javascript app is offline?如何检测javascript应用程序是否离线? Meaning cannot reach the server and load the templates.意思是无法访问服务器并加载模板。

I finally found a JSFiddle that does exactly that: Check if the web application is online.我终于找到了一个可以做到这一点的 JSFiddle:检查 Web 应用程序是否在线。 It uses window.navigator.onLine .它使用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

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM