使用离子框架中的ng repeat显示预填充的sqlite db in list中的数据

时间:2016-11-24 18:06:02

标签: android angularjs sqlite ionic-framework ng-repeat

我是初学者。使用离子1,我想使用ng repeat显示列表中数据库的数据,结果不显示在视图中(html)!!! (genymotion模拟器为android) 我已经尝试过教程,但没有成功。

我的代码:

Controllers.js

.controller('ExampleController', function($scope, $cordovaSQLite) {

$scope.allSessions = [];

$scope.selectAll = function() {

var query = "SELECT * FROM meds";

$cordovaSQLite.execute(db, query, []).then(function(res) {

    if(res.rows.length > 0) {
         console.log("SELECTED -> " + res.rows.item(0).id + " " + res.rows.item(0).nom);
         for (var i=0; i<res.rows.length; i++) {

            $scope.allSessions.push({
                id: res.rows.item(i).id,
                nom: res.rows.item(i).nom
                })
         }
    } else {
        console.log("No results found");
    }
}, function (err) {
    console.error("error=>"+err);
});
}

app.js

var Myapp = angular.module('app', ['ionic', 'ngCordova',        'app.controllers']);
var db = null;
Myapp.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();
}
window.plugins.sqlDB.copy("med.db",0, function() {
        db = $cordovaSQLite.openDB({name: 'med.db', location: 'default'});
    }, function(error) {
        console.error("There was an error copying the database: " + error);
        db = $cordovaSQLite.openDB({name: 'med.db', location: 'default'});
    });
 });
 });

meds.html

<div>
<ion-list>
<ion-item ng-repeat="session in allSessions" href="#/app/session/{{session.id}}">
<h3>{{session.id}} {{session.nom}}</h3>
</ion-item>
</ion-list>
</div>

需要帮助

2 个答案:

答案 0 :(得分:0)

做到:

<强> Controller.js

.controller('ExampleController', function($scope, $cordovaSQLite) {

$scope.allSessions = [];

$scope.selectAll = function() {

var query = "SELECT * FROM meds";

$cordovaSQLite.execute(db, query, []).then(function(res) {     
    if(res.rows.length > 0) {
       angular.forEach(res.rows, function(item)){
          $scope.allSessions.push({
             id: item.id,
             nom: item.nom
          })
    }

    } else {
        console.log("No results found");
    }
}, function (err) {
    console.error("error=>"+err);
});
}

答案 1 :(得分:0)

.controller('ExampleController', function($scope, $cordovaSQLite) {

$scope.selectAll = function() {

var query = "SELECT * FROM meds";

$cordovaSQLite.execute(db, query, []).then(function(res) {     
if(res.rows.length > 0) {
 $scope.allSessions = [];
  for (var i = 0; i<res.rows.length; i++){
      $scope.allSessions.push({
         id: res.rows.item(i).id,
         nom: res.rows.item(i).nom
      })
 }

 } else {
    console.log("No results found");
 }
 }, function (err) {
console.error("error=>"+err); 
 });
}

然后在视图中使用ng repeat传递。这应该在2017年完美。在这里输入代码