2016-04-03 79 views
1

我已经安装使用这个指令sqlite的科尔多瓦插件如下: http://ngcordova.com/docs/plugins/sqlite/

请看下面的控制器代码:

.controller('PlaylistsCtrl', function ($scope, $cordovaSQLite) { 
    $scope.playlists = [ 
     { title: 'Reggae', id: 1 }, 
     { title: 'Chill', id: 2 }, 
     { title: 'Dubstep', id: 3 }, 
     { title: 'Indie', id: 4 }, 
     { title: 'Rap', id: 5 }, 
     { title: 'Cowbell', id: 6 } 
    ]; 

    //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); 
    // }); 
    //}; 

}) 

错误:

enter image description here

+1

您注射了ngCordova吗?例如'angular.module('starter.controllers',['ngCordova'])' – Huey

+0

谢谢。这是问题!我已经提到https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/ –

回答

3

我通过引用此链接解决了错误:https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/

步骤-1:下载此文件:Link并将ng-cordova.min.js复制到您的“www/js”目录中。

2步:在cmd中使用此命令

cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin.git 

步3:打开index.html文件并添加以下行:

<script src="js/ng-cordova.min.js"></script> 

这是非常重要的,你加它位于cordova.js行的上方,否则它将无法工作。

第4步:在我们开始使用ngCordova之前,还需要添加一件东西。我们需要把它注入到我们的angular.module,在app.js中发现,类似如下:

angular.module('starter', ['ionic', 'ngCordova']) 

就是这样。现在sqlite是可用的。