我尝试编写一个控制是否存在互联网连接的拦截器。如果连接不存在,它将显示一条消息,"您没有连接"并且它会中止http请求,否则它将进行htt prequest.How我能做到这一点吗? 提前致谢
答案 0 :(得分:1)
您应该使用以下插件:cordova plugin add cordova-plugin-network-information
代码示例:
module.controller('MyCtrl', function($rootScope, $cordovaNetwork) {
document.addEventListener("deviceready", function () {
var type = $cordovaNetwork.getNetwork()
var isOnline = $cordovaNetwork.isOnline()
var isOffline = $cordovaNetwork.isOffline()
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
var onlineState = networkState;
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
var offlineState = networkState;
})
}, false);
});