2016-09-26 45 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

相关问题