sqlite选择的数据没有显示给ng重复

sqlite selected data not showing to ng-repeat

本文关键字:显示 ng 重复 选择 数据 sqlite      更新时间:2024-02-06

我正在调用listItems.push(res.rows.item(I).name)中的数据;通过ng repeat添加到列表中。所以我得到了一个SQLite中存在的名称列表。

这是我的控制器.js

$scope.createList = function () {
    var value1 = document.getElementById("List").value;
    alert(value1);
    var db = $cordovaSQLite.openDB({ name: "Bd_DB.db" });
    var query = "INSERT INTO List (name) VALUES (?)";
    $cordovaSQLite.execute(db, query, [value1]).then(function (res) {
        console.log("insertId: " + res.insertId);
    }, function (err) {
        alert(err);
        console.error(err);
    });
    $scope.getAllLists();
};
$scope.getAllLists = function () {
    var listItems= [];
    var query = "SELECT * FROM List";
    $cordovaSQLite.execute($cordovaSQLite.openDB({ name: "Bd_DB.db" }), query).then(function (res) {
        if (res.rows.item(0).name !="") {
            for (var i = 0; i < res.rows.length; i++) {
              listItems.push(res.rows.item(i).name);

            }
        }
    });
}

我试过了,但它没有显示任何类型的列表。我不知道,我做错了什么。请告诉我正确的代码。谢谢这是我的Html:

 <div class="bar bar-header">
  <button class="button icon-left ion-chevron-left button-clear button-dark"            ng-click="closeModal()" ></button>
  <h1 class="title">Your List</h1>
  <button class="button" ng-click="createListPopup()"> Create List</button>
</div>

我的待办事项列表

<ion-content>

      <div ng-repeat= "name in listItems">
    {{item.name}}
  </div>

</ion-content>

sqlite事务是同步的,因此在创建列表中,您必须在execute的成功回调中调用getList:比如:$scope.createList=函数(){

var value1 = document.getElementById("List").value;
alert(value1);
var db = $cordovaSQLite.openDB({ name: "Bd_DB.db" });
var query = "INSERT INTO List (name) VALUES (?)";
$cordovaSQLite.execute(db, query, [value1]).then(function (res) {
    console.log("insertId: " + res.insertId);
     $scope.getAllLists();
}, function (err) {
    alert(err);
    console.error(err);
});

};

此外,在getAllLists中,无论.execute的状态如何,函数都将结束,因此您必须绑定到成功回调中设置的变量。

$scope.listItems= [];

$scope.getAllLists=函数(){

var query = "SELECT * FROM List";
$cordovaSQLite.execute($cordovaSQLite.openDB({ name: "Bd_DB.db" }), query).then(function (res) {
    $scope.listItems= [];
    if (res.rows.item(0).name !="") {
        for (var i = 0; i < res.rows.length; i++) {
          listItems.push(res.rows.item(i).name);
        }
        $scope.$apply();
    }
}

最后,您必须使$scope$apply()将结果反映到视图中。

尝试将var listItems= []更改为范围变量

$scope.listItems = [];
....
$scope.listItems.push({name:'item 1'})

然后在你看来:

<ion-content>
  <div ng-repeat= "item in listItems">
      {{item.name}}
  </div>
</ion-content>