我有基于 Cordova 和 Ionic 的移动应用程序。在应用程序启动后加载的默认页面上,需要使用 SQLLite 插件。
https://github.com/brodysoft/Cordova-SQLitePlugin
问题是 View 包含
ng-init="setData()"
调用与 SQL Lite 插件一起工作的 Controller 方法。并且由于该方法是在deviceready事件未初始化之前调用的(插件只能在deviceready事件之后初始化)。
所以我尝试了这个解决方法:
.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"});
}
但这对我不起作用。
所以我尝试了第二种解决方案:
.factory('cordova', function () {
return {
test: function(){
document.addEventListener("deviceready", this.ready, false);
},
ready: function(){
alert("Ready");
db = window.sqlitePlugin.openDatabase({name:"callplanner"});
}
}
})
在 Controller 初始化中我尝试了:
cordova.test();
但这不起作用(在 ng-init 之后触发 devicereadfy)。
然后我找到了这篇文章:
http://java.dzone.com/articles/ionic-and-cordovas-deviceready
但我不明白如何在应用准备好之前放置“启动画面”以及如何设置超时。
有人知道我该如何解决这个问题吗?
非常感谢您的任何建议或帮助。
最佳答案
您需要反转它,首先处理 cordova“deviceready”事件,然后启动 angularjs 应用程序。像这样:
首先从 html/body 标签中移除 ng-app 属性
在设备就绪后启动 Angular 应用程序:
<script> document.addEventListener('deviceready', function() { angular.bootstrap(document, ['YourAppName']); }, false); var YourAppName = angular.module('YourAppName', []); </script>
类似问题:
关于javascript - 如何在 Ionic 应用程序中以正确的方式准备好设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300583/