当我在onDeviceReady: function ()
内使用sqliteplugin之类的第一个例子时,它运行正常。但是当我在控制器中使用第二个例子时,它不起作用。
TypeError: cordova.exec is not a function.
示例1
var app = angular.module("myApp", []);
app = {
// Application Constructor
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady);
},
onDeviceReady: function () {
function copyDatabaseFile(dbName) {
var sourceFileName = cordova.file.applicationDirectory + 'www/' + dbName;
var targetDirName = cordova.file.dataDirectory;
return Promise.all([
new Promise(function (resolve, reject) {
resolveLocalFileSystemURL(sourceFileName, resolve, reject);
}),
new Promise(function (resolve, reject) {
resolveLocalFileSystemURL(targetDirName, resolve, reject);
})
]).then(function (files) {
var sourceFile = files[0];
var targetDir = files[1];
return new Promise(function (resolve, reject) {
targetDir.getFile(dbName, {}, resolve, reject);
}).then(function () {
//console.log("file already copied");
}).catch(function () {
console.log("file doesn't exist, copying it");
return new Promise(function (resolve, reject) {
sourceFile.copyTo(targetDir, dbName, resolve, reject);
}).then(function () {
console.log("database file copied");
});
});
});
}
copyDatabaseFile('mydatabase.db').then(function () {
// success! :)
var db = sqlitePlugin.openDatabase('mydatabase.db');
if(window.cordova)
db = sqlitePlugin.openDatabase('mydatabase.db');
else
db = sqlitePlugin.openDatabase('mydatabase.db', '1','my', 1024*1024*100);
db.readTransaction(function (txn) {
txn.executeSql('SELECT * FROM mytable', [], function (tx, res) {
console.log('Successfully read from pre-populated DB:');
console.log(JSON.stringify(res));
});
});
}).catch(function (err) {
// error! :(
console.log(err);
});
},
}
};
app.initialize();
示例2
app.controller("Tab4Ctrl", function($scope) {
var db = sqlitePlugin.openDatabase('mydatabase.db');
if(window.cordova)
db = sqlitePlugin.openDatabase('mydatabase.db');
else
db = sqlitePlugin.openDatabase('mydatabase.db', '1','my', 1024*1024*100);
db.readTransaction(function (txn) {
txn.executeSql('SELECT * FROM mytable', [], function (tx, res) {
console.log(JSON.stringify(res));
});
});
});
答案 0 :(得分:0)
I have found my mistake
app.controller("Tab4Ctrl",['$scope', function($scope, $sqliteplugin) {
var db = sqlitePlugin.openDatabase('mydatabase.db');
if(window.cordova)
db = sqlitePlugin.openDatabase('mydatabase.db');
else
db = sqlitePlugin.openDatabase('mydatabase.db', '1','my', 1024*1024*100);
db.readTransaction(function (txn) {
txn.executeSql('SELECT * FROM mytable', [], function (tx, res) {
console.log(JSON.stringify(res));
});
});
});
}])