2016-04-03 103 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是可用的。