如何Deviceready在正确的方式在Ionic应用程序? [英] How to Deviceready in right way in Ionic application?

查看:697
本文介绍了如何Deviceready在正确的方式在Ionic应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于Cordova和Ionic的移动应用程序。在应用程序启动后加载的默认页面上需要使用SQLLite插件。

I have Cordova and Ionic based mobile application. On the default page which is loaded after the start of the application is need to work with SQLLite plugin.

https://github.com/brodysoft/Cordova-SQLitePlugin

问题是视图包含

ng-init="setData()"

这是使用SQL Lite插件调用控制器方法。因为方法是在deviceready事件未初始化之前调用的(插件只能在deviceready事件之后初始化)。

Which is calling the controller method where is worked with SQL Lite plugin. And because of the the method is called before the deviceready event is not initialized (plugin can be initialized only after deviceready event).

所以我试过这个解决方法:

So I tried this workaround:

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      db = window.sqlitePlugin.openDatabase({name:"callplanner"});
    }

但是这对我来说不行。



So i tried second solution:

.factory('cordova', function () {
  return {
      test: function(){
          document.addEventListener("deviceready", this.ready, false);
      },
      ready: function(){
            alert("Ready");
            db = window.sqlitePlugin.openDatabase({name:"callplanner"});
      }

  }
})

in controller init我试过:

and in controller init i tried:

cordova.test();

但是这是不起作用(devicereadfy是在ng-init之后触发的)。

But this is not working to (devicereadfy is fired after ng-init).

之后,我发现了这篇文章:

After that i found this article:

http://java.dzone.com/articles/ionic-and-cordovas-deviceready

但我没有了解如何在应用程序就绪之前设置闪屏,以及如何设置超时。

But i did not understand how to put "splash screen" before app is ready and how to set timeout.

有人想法如何解决这个问题?

Have somebody idea how can I solve this problem?

非常感谢任何建议或帮助。

Many Thanks for any advice or help.

推荐答案

您需要反转这一点, cordovadeviceready事件,然后你启动angularjs应用程序。像这样:

You need to invert this, first you handle the cordova "deviceready" event and then you start the angularjs app. Like this:


  1. 首先从html / body标签中删除ng-app属性

  1. First remove the the ng-app attribute from the html/body tag

在devireready之后启动angular应用程序:

Start the angular app after the devireready:

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


类似问题:

  • Cordova + Angularjs + Device Ready
  • Initialize my angularJs App after Phonegap deviceready

这篇关于如何Deviceready在正确的方式在Ionic应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆