我目前正在尝试使用我的应用程序实现 ngCordova SQLite 插件,但尚未产生有效的解决方案。我已经关注了Nic Raboy 的博客文章,关于如何将 SQLite 插件与您的 Ionic 项目实现为“T”,但我仍然收到错误:错误:未定义不是对象(评估“$window.sqlitePlugin.openDatabase”)当我尝试在 iOS 模拟器中运行应用程序时。
我还验证了 ngCordova 和插件已加载到我的项目中。
这是我的脚本如何加载到我的项目中的顺序:
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
我的代码如下。
应用程序.js
angular.module('whoPaidLast', ['ionic', 'ngCordova', 'whoPaidLast.controllers', 'whoPaidLast.services'])
.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
db = $cordovaSQLite.openDB({ name: 'accounts.db' });
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS accounts (id integer primary key autoincrement, firstname text, lastname text, paid numeric, date text)');
});
})
控制器.js
angular.module('whoPaidLast.controllers', [])
.controller('DashCtrl', ['$ionicPlatform', '$scope', '$ionicModal', '$ionicActionSheet', '$cordovaSQLite', function ($ionicPlatform, $scope, $ionicModal, $ionicActionSheet, $cordovaSQLite) {
$scope.getList = function() {
var query = 'SELECT id, firstname, lastname, paid, date FROM accounts ORDER BY lastname ASC';
var db = $cordovaSQLite.openDB({ name: 'accounts.db' });
$ionicPlatform.ready(function() {
$cordovaSQLite.execute(db, query, [id, firstname, lastname, paid, date]).then(function(result) {
if(result.rows.length > 0) {
console.log('rows =' + result.rows);
} else {
$scope.results = [];
}
}, function(error) {
console.error(err);
});
});
};
});
您可能拥有的任何信息或帮助都会有所帮助。提前谢谢。