簡體   English   中英

角度:未捕獲ReferenceError:未定義$ cordovaSQLite

[英]Angular: Uncaught ReferenceError: $cordovaSQLite is not defined

嘗試遵循使用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);

}

但是,我得到了錯誤

未捕獲的ReferenceError:未定義$ cordovaSQLite

services.js如何繼承app.js中定義的$ cordovaSQLite?

像這樣將$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模塊ngCordovaangular.module('starter.services', ['ngCordova'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM