2016-09-26 72 views
0

Im新的IONIC和我需要使用ngCordova的幫助我能夠在我的app.js.上創建數據庫。但是我總是在嘗試時收到此錯誤消息訪問我的控制器上的數據庫「類型錯誤:無法調用未定義的方法 '的openDatabase'」IONIC上的SQLite「TypeError:無法調用未定義的方法'openDatabase'

下面是我的代碼:

Controller.js:

angular.module('app.controllers', ['ionic','ngCordova']) 

.controller('loginCtrl', ['$scope', 'LoginService', '$ionicPopup', '$state','$cordovaSQLite', 
function ($scope,LoginService,$ionicPopup, $state,$cordovaSQLite) { 

$scope.data = {}; 

$scope.login = function() { 
    var db = null; 
    console.log("LOGIN user: " + $scope.data.username + " - PW: " + $scope.data.password); 
    LoginService.loginUser($scope.data.username, $scope.data.password).success(function(data) { 
     db = $cordovaSQLite.openDB("guards.db"); 

         var query = "CREATE TABLE IF NOT EXISTS incident_list (id integer autoincrement primary key, complainant string , defendant string, details string, remarks string,incident_date datetime)"; 
         runQuery(query,[],function(res) { 
          console.log("table created done"); 
         }, function (err) { 
          console.log("table created done"); 
          console.log(err); 
         }); 
     $state.go('guardTrack.home'); 
    }).error(function(data) { 
     var alertPopup = $ionicPopup.alert({ 
      title: 'Login failed!', 
      template: 'Please check your credentials!' 
     }); 
    }); 
}}]) 

回答

0

Check the plugin usage in ngCordova docs you include the sqlite in your controller in order to use it.

module.controller('MyCtrl', function($scope, $cordovaSQLite) { 
    var db = $cordovaSQLite.openDB({ name: "my.db" }); 

// for opening a background db:

var db = $cordovaSQLite.openDB({ name: "my.db", bgType: 1 }); 
    $scope.execute = function() { 
    var query = "INSERT INTO test_table (data, data_num) VALUES (?,?)"; 
    $cordovaSQLite.execute(db, query, ["test",  100]).then(function(res) { 
     console.log("insertId: " + res.insertId); 
    }, function (err) { 
     console.error(err); 
    }); 
    }; 
}); 

幫助Link

相關問題