简体   繁体   English

科尔多瓦角离线检查

[英]Cordova angular offline check

I have below code to check online/offline in my cordova application. 我有以下代码在我的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;
}  

And my angular controller is like below. 我的角度控制器如下所示。

 .controller('MainCtrl',['$scope','$http','$localStorage','$state',function($scope, $http, $localStorage, $state){ 


  if(online==true){

    //code for online
  }else{
    // code for offline
   } 


}])

I called the checking in 'deviceready' event,the status getting .but my problem is - deviceready is called after my controller is started.is it possible to check the network status before angular controller started execution? 我将检查称为“ deviceready”事件,状态变为。但是我的问题是-我的控制器启动后调用deviceready是否可以在角度控制器开始执行之前检查网络状态?

In Angular, controller are executed during the running phase of your application. 在Angular中,控制器在应用程序的运行阶段执行。 You can execute some code before that running phase, also called configuration phase . 您可以在该运行阶段(也称为配置阶段)之前执行一些代码。

You shoudl read module documentation from angular. 您应该从角度阅读模块文档

  • Configuration blocks - get executed during the provider registrations and configuration phase. 配置块-在提供者注册和配置阶段执行。 Only providers and constants can be injected into configuration blocks. 只有提供者和常量可以注入配置块。 This is to prevent accidental instantiation of services before they have been fully configured. 这是为了防止在服务完全配置之前意外实例化服务。

  • Run blocks - get executed after the injector is created and are used to kickstart the application. 运行块-创建注射器后执行,并用于启动应用程序。 Only instances and constants can be injected into run blocks. 只能将实例和常量注入运行块中。 This is to prevent further system configuration during application run time. 这是为了防止在应用程序运行期间进行进一步的系统配置。

In your code, it should looks like this : 在您的代码中,它应如下所示:

angular.module('myModule', []).
config(function(injectables) { // provider-injector
  // This is an example of config block.
  // You can have as many of these as you want.
  // You can only inject Providers (not instances)
  // into config blocks.
}).
run(function(injectables) { // instance-injector
  // This is an example of a run block.
  // You can have as many of these as you want.
  // You can only inject instances (not Providers)
  // into run blocks
});

Not sure if I understood well, but that might let you execute code before running your controller :) 不知道我是否了解得很好,但这可能会让您在运行控制器之前执行代码:)

Just on thought about your code ... config will be run only once and your connection can change over time (if in a car, going from 4G to 3G). 考虑一下您的代码...配置将只运行一次,并且您的连接会随着时间而变化(如果在汽车中,从4G变为​​3G)。 So might need to check regularely or check in your controller. 因此可能需要定期检查或检查您的控制器。 Best case could be a function you call at the activation of your contoller. 最好的情况可能是在激活控制器时调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM