gpt4 book ai didi

javascript - Angular : Uncaught ReferenceError: $cordovaSQLite is not defined

转载 作者:行者123 更新时间:2023-11-28 18:42:00 25 4
gpt4 key购买 nike

尝试遵循使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com