【问题标题】:detecting network after view loaded in ionic app在离子应用程序中加载视图后检测网络
【发布时间】:2015-03-20 01:21:19
【问题描述】:

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

我的控制器中有以下代码

.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 等

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

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

【问题讨论】:

    标签: angularjs cordova ionic-framework


    【解决方案1】:

    在设备准备好之前不要启动您的应用

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

    【讨论】:

    • 我必须把这个放在哪里?在 app.js 中?
    • 我不知道你的应用结构,无论模块在哪里调用
    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多