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

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

问题描述

我有基于 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"});
      }

  }
})

在控制器初始化中我试过:

and in controller init i tried:

cordova.test();

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

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>

类似问题:

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

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