尝试遵循使用 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 如何继承 app.js 中定义的 $cordovaSQLite?
最佳答案
注入(inject)$cordovaSQLite
服务到您factory
像这样:
angular.module('starter.services', [])
.factory('Geolocation', ['$cordovaSQLite', function($cordovaSQLite) {
//do something with $cordovaSQLite
}]);
以下是我使用的语法的更多信息: https://docs.angularjs.org/guide/di#dependency-annotation
编辑:
您的模块starter.services
也可能缺少 cordova 模块 ngCordova
:
angular.module('starter.services', ['ngCordova'])
关于javascript - Angular : Uncaught ReferenceError: $cordovaSQLite is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35999589/