在离子应用程序中加载视图后检测网络

detecting network after view loaded in ionic app

我正在使用离子框架。我想要的是,如果网络可用,应用程序应该显示一张卡,如果没有网络可用,应用程序应该显示另一张卡。

我的控制器中有以下代码

.controller('PlaylistsCtrl', function($scope) {

init=function(){
 alert(navigator.connection.type);
}

  init();
})

警报 returns 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 等

所以我认为 angular 在 navigator.connection.type 解决之前正在执行。

如何在查看负载时检测网络?

在设备准备就绪之前不要启动您的应用程序

  document.addEventListener('deviceready', function() { 
    angular.bootstrap(document, ['YourAppName']);
  }, false);
  var YourAppName = angular.module('YourAppName', []);