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

标签 javascript angularjs ionic-framework

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

相关文章:

javascript - 让这些箭头滚动? jquery 和 html

java - HTTP 状态 403 - 在请求参数 '9ee6949c-c5dc-4d4b-9d55-46b75abc2994' 或 header '_csrf' 上发现无效的 CSRF token 'X-CSRF-TOKEN'

javascript - 使用 ngStorage/localstorage 保存键=>值样式

javascript - JSON 解析不适​​用于 ng-repeat

javascript - 如何在 ionic 应用程序中设置起始页面?

javascript - 如何从模式弹出文本字段的无序列表中编辑特定列表项

javascript - Ng-view on load 事件

javascript - 无法在桌面上为远程 IP 文件创建快捷方式

javascript - 使用 ng-repeat Angular.js 创建网格

javascript - BLOB 对象不可见