2016-01-06 16 views
0

這是代碼的部分我已經查明,以引起問題的對象:cordovaSQLite不工作,錯誤:未定義不是(評價「n.transaction」)

angular.module('starter.controllers', []).controller('StudentsCtrl', function ($scope, $cordovaSQLite) { 

    var query = "SELECT id, student_id, username FROM students"; 
    var users = []; 

    $cordovaSQLite.execute(db, query).then(function (data) { 
    $.each(data, function(i, item) { 
     users.push(item); 
    }); 
    }); 

    $scope.users = users; 
}); 

我已經包括必要的文件爲$cordovaSQLite工作。以上代碼位於www/js/controllers.js之內,作爲默認Ionic選項卡式項目的一部分。 $cordovaSQLite工作正常的www/js/app.js內,也是在www/js/controllers.js另一部分工作正常,但上面的代碼段返回我這個錯誤:

0  610246 error Error: undefined is not an object (evaluating 'n.transaction') 

更新:看來,$cordovaSQLite等於undefined在這裏這個功能,但我不確定爲什麼會發生這種情況。

回答

1

在Cordova deviceready事件觸發之前,您的代碼正在運行。在使用任何設備功能(如插件)之前,您需要等待它。離子,如果你在你的控制器使用$ ionicPlatform,你可以這樣做:

$ionicPlatform.ready(function() {

,以此來包裝需要等待它的代碼。試試這個:

angular.module('starter.controllers', []).controller('StudentsCtrl,$ionicPlatform', function ($scope, $cordovaSQLite) { 
 

 
    $ionicPlatform.ready(function() { 
 
    var query = "SELECT id, student_id, username FROM students"; 
 
    var users = []; 
 

 
    $cordovaSQLite.execute(db, query).then(function (data) { 
 
     $.each(data, function(i, item) { 
 
     users.push(item); 
 
     }); 
 
    }); 
 

 
    $scope.users = users; 
 
    
 
    }); 
 
    
 
});

+0

就像一個魅力。感謝你給我的代碼,也爲了解釋它! :d – think123

相關問題