我有以下代碼來在線/離線檢查我的cordova應用程序。Cordova角度離線檢查
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown';
states[Connection.ETHERNET] = 'Ethernet';
states[Connection.WIFI] = 'WiFi';
states[Connection.CELL_2G] = 'Cell2G';
states[Connection.CELL_3G] = 'Cell3G';
states[Connection.CELL_4G] = 'Cell4G';
states[Connection.CELL] = 'Cellgeneric';
states[Connection.NONE] = 'Nonetwork';
alert(states[networkState]);
if(states[networkState]!='Nonetwork'){
online=true;
}else{
online=false;
}
而我的角度控制器就像下面。
.controller('MainCtrl',['$scope','$http','$localStorage','$state',function($scope, $http, $localStorage, $state){
if(online==true){
//code for online
}else{
// code for offline
}
}])
我叫「deviceready」事件的檢查,狀態得到。但我的問題是 - deviceready
被稱爲後我控制器started.is可以檢查網絡狀態前角控制器開始執行的?
在你的例子中,我看不到'deviceready'被調用的地方。可能會有一些元素缺失。你在哪裏叫第一塊代碼檢查網絡? – sebastienbarbier