我在 ionic 应用程序中嵌入 SQLite 时遇到此错误,我已阅读所有相关文章,但没有任何帮助解决此错误仍然存在。我正在分享我的 app.js、controller.js 和 html 文件。
<小时/>app.js
var starter=angular.module ('starter', ['ionic', 'ngCordova','starter.controllers']);
var db=null;
starter.run(
function ($ionicPlatform, $cordovaSQLite)
{
$ionicPlatform.ready
(
function()
{
if (window.cordova && window.cordova.plugins.Keyboard)
{
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar)
{
StatusBar.styleDefault();
}
if(window.sqlitePlugin)
{
window.plugins.sqlDB.copy
(
"Tour_MobileApp.db"
,function ()
{
db = window.sqlitePlugin.openDatabase({name: "Tour_MobileApp.db"});
}
, function()
{
console.error("There was an error copying the database: " + error);
db = window.sqlitePlugin.openDatabase({name: "Tour_MobileApp.db"});
}
);
}
}
);
}); starter.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state
(
'app',
{
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
}
)
.state
(
'app.home',
{
url: '/home',
views:
{
'menuContent':
{
templateUrl: 'templates/home.html',
controller: 'homeCtrl'
}
}
}
)
.state
(
'app.places',
{
url: '/places',
views:
{
'menuContent':
{
templateUrl: 'templates/places.html',
controller: 'placesCtrl'
}
}
}
)
.state
(
'app.login',
{
url: '/login',
views:
{
'menuContent':
{
templateUrl: 'templates/login.html',
}
}
}
)
.state
(
'app.browse',
{
url: '/browse',
views:
{
'menuContent':
{
templateUrl: 'templates/browse.html',
}
}
}
)
.state
(
'app.rfort',
{
url: '/rfort',
views:
{
'menuContent':
{
templateUrl: 'templates/rfort.html',
}
}
}
)
.state
(
'app.ltemple',
{
url: '/ltemple',
views:
{
'menuContent':
{
templateUrl: 'templates/ltemple.html',
}
}
}
)
.state
(
'app.igate',
{
url: '/igate',
views:
{
'menuContent':
{
templateUrl: 'templates/igate.html',
}
}
}
)
.state
(
'app.jmasjid',
{
url: '/jmasjid',
views:
{
'menuContent':
{
templateUrl: 'templates/jmasjid.html',
}
}
}
)
.state
(
'app.guide',
{
url: '/guide',
views:
{
'menuContent':
{
templateUrl: 'templates/guide.html',
}
}
}
)
$urlRouterProvider.otherwise('/app/home');
});
Controller .js
angular.module('starter.controllers', []) .controller('AppCtrl',function($scope) {}) .controller('homeCtrl',function($scope, $stateParams) {}).controller("placesCtrl", function($scope, $cordovaSQLite) {$scope.categories = [];
$scope.refresh = function() {
var query = "SELECT Place_Name, Place_Detail FROM Places_of_interest ORDER BY Place_Name ASC";
$cordovaSQLite.execute(db, query, []).then(function(res) {
if(res.rows.length > 0) {
for(var i = 0; i < res.rows.length; i++) {
$scope.categories.push({place_Name: res.rows.item(i).place_Name, Place_Detail: res.rows.item(i).Place_Detail});
console.log("SELECTED -> " + res.rows.item(i).Place_Name + " " + res.rows.item(i).Place_Detail);
}
} else {
console.log("No results found");
}
}, function (err) {
console.error(err);
});
}});
地点.html
<ion-view view-title="Places of Interest"><ion-content ng-controller="placesCtrl"><ion-list>
<button class="button button-small button-balanced"ng-click="refresh()" >
Referesh
</button>
<ion-item ng-repeat="category in categories">
{{category.Place_Name}} {{category.Place_Detail}}
</ion-item>
</ion-list>
最佳答案
在设备上,您必须使用以下命令打开数据库:
db = $cordovaSQLite.openDB("myDb");
关于angularjs - 在 IONIC 应用程序中使用 SQLite 时,"TypeError: Cannot read property ' 事务 ' of null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35915542/