我正在使用离子框架。我想要的是,如果网络可用,应用程序应显示一张卡,如果没有网络可用,则显示其他卡。
我的控制器中有以下代码
.controller('PlaylistsCtrl', function($scope) {
init=function(){
alert(navigator.connection.type);
}
init();
})
警报返回0零,不知道为什么会这样。
所以我试图连接按钮并看到相同的代码
<button ng-click="demo()" class="button button-positive">get network</button>
并在控制器中
.controller('PlaylistsCtrl', function($scope) {
$scope.demo=function(){
alert(navigator.connection.type);
}
})
现在我确实得到了正确的结果,所以我得到了wifi,2g等
所以我认为在解析navigator.connection.type之前会执行angular。
如何在视图加载时检测网络?
答案 0 :(得分:2)
在设备准备好之前,不要启动应用
document.addEventListener('deviceready', function() {
angular.bootstrap(document, ['YourAppName']);
}, false);
var YourAppName = angular.module('YourAppName', []);