我有一个函数init DB
this.db = $cordovaSQLite.openDB({ name: "pasa.db" });
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS dashboard (id integer primary key, orders_open integer,orders_complete integer,orders_all integer,alerts integer)');
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS orders (reference text primary key not null, first_name text,last_name text,delivery_address_country_code text,state text,merchant_price text)')
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS products (permalink text primary key not null, title text,active text)')
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS product_details (permalink text primary key not null, details text)')
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS order_details (permalink text primary key not null, details text)')
我希望在我的代码执行或路由器中的解析之前执行此功能。
$ionicPlatform.ready(function() {
DatabaseService.initDB();
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
在这方面,请你帮我解决我的init db在其他任何事情之前被调用的问题,或者是阻塞调用
答案 0 :(得分:3)
我遇到了同样的问题,现在工作正常
<强> app.js 强>
var db = null;
angular.module('moduleName',
['ionic',
'ngCordova',
'db.service'
]
)
.run(function($ionicPlatform,$cordovaSQLite,DB) {
$ionicPlatform.ready(function() {
/*calling service function for initialise database*/
DB.init();
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
<强> db.service.js 强>
angular.module('db.service',
[
'db.config',
'ngCordova'
]
)
/* DB service definition goes here*/
.factory('DB', function($q,DB_CONFIG,$cordovaSQLite) {
var self = this;
self.db = null;
/* init function*/
self.init = function() {
self.db = $cordovaSQLite.openDB(DB_CONFIG.name+".db");
angular.forEach(DB_CONFIG.tables, function(table) {
var columns = [];
angular.forEach(table.columns, function(column) {
columns.push(column.name + ' ' + column.type);
});
var dbQuery = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
$cordovaSQLite.execute(self.db,dbQuery)
.then(
function(res) {
alert("Tables are created");
},function (err) {
alert(err);
}
);
});
};
return self;
})
<强> db.config.js 强>
/*database configuration goes here,eg:list of tables with columns*/
angular.module('db.config', [])
.constant('DB_CONFIG', {
name: 'my',
tables: [
{
name: 'people',
columns:[
{name: 'id', type: 'integer primary key'},
{name: 'firstname', type: 'text'},
{name: 'lastname', type: 'text'}
]
}
]
});
答案 1 :(得分:0)
我也解决了这个问题:
<强> app.js:强>
$ionicPlatform.ready(function () {
db = $cordovaSQLite.openDB("my.db");
alert("HERE 1");
});
<强>控制器:强>
$ionicPlatform.ready(function () {
alert("HERE 2");
// SELECT
});
因此警报1出现在警报2之前