试图遵循使用SQL lite的例子,但打了一个残端。
举例说,包括ngCordova在app.js这样:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])
那部分作品。
然后在.RUN指令创建DB +表:
db = $cordovaSQLite.openDB("space.db");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS locations (id integer primary key, latitude text, longitude text, altitude text, speed text, date text, time text)");
那部分工作,以及...
然后,在services.js我想添加一条记录时,我得到的GPS位置。
我这样做,使用此代码在services.js:
angular.module('starter.services', [])
.factory('Geolocation', function() {
var callBackFunction ;
var latitude, longitude, altitude ;
function getPosition(){
return new Array(latitude, longitude, altitude, speed, GPSEnabled);
}
function showPosition(position) {
latitude = position.coords.latitude ;
longitude = position.coords.longitude ;
altitude = position.coords.altitude ;
speed = position.coords.speed ;
var query = "INSERT INTO locations (latitude, longitude, altitude, speed) VALUES (?,?,?,?)";
$cordovaSQLite.execute(db, query, [latitude, longitude, altitude, speed, date, time]).then(function(res) {
console.log("INSERT ID -> " + res.insertId);
}, function (err) {
console.error(err);
});
callBackFunction();
}
navigator.geolocation.getCurrentPosition(showPosition, showError, options);
}
但是,我得到的错误
Uncaught ReferenceError: $cordovaSQLite is not defined
如何services.js继承的已定义是$ cordovaSQLite在app.js?
你需要将它注入你的工厂函数〜'.factory('Geolocation',function($ cordovaSQLite){...})'。我假设你已经在你的'run'函数中做了 – Phil