AngularJS ng-repeat在更新时不起作用

AngularJS ng-repeat on update not working

本文关键字:不起作用 更新 ng-repeat AngularJS      更新时间:2023-09-26

我正在尝试在HTML中显示将值推入的数组中的元素。这些值只是基本字符串。

这是设置数据库的代码:

$scope.loadcourse = function() {
var db = null;
document.addEventListener("deviceready", function(){
db = window.sqlitePlugin.openDatabase({name: "note.db"});
db.transaction(function(tx) {
    tx.executeSql("CREATE TABLE IF NOT EXISTS note (name text primary key, data text)");
    alert("Loaded DB");
}, function(err){
    alert("An error occurred while initializing the app");
});
$scope.$apply(function(){
  $scope.courseDB = ["1","2"];
});
db.transaction(function(tx) {
      tx.executeSql("SELECT (name) FROM note", [], function(tx,res){
          for(var iii = 0; iii < res.rows.length; iii++)
          {
              $scope.courseDB.push(res.rows.item(iii).name);
              alert($scope.courseDB);
          }
      });
  }, function(err){
      alert("An error occured while displaying saved notes");
  });

}, false);
}

这是我显示条目的方式:

<ion-content ng-controller="FeedController" ng-init="loadcourse()" class="has-tabs-top">
    <ion-list>
      <ion-item ng-repeat="entry in courseDB">
        Hello, {{entry}}!
      </ion-item>
    </ion-list>
  </ion-content>

现在,没有意义的是,它只显示两个虚拟值(1 和 2),但不显示 loadCorse 函数内推送函数中添加的元素 - 即使被推送到数组中的元素,在出现的警报弹出窗口中可见!

我几乎 100% 确定我只是在这里错过了一个愚蠢的小细节。

更新

这是阵列在警报时的外观:(无括号)

1, 2, 文本字符串

, 文本字符串, 文本字符串

您应该触发 $scope.$apply() 来触发更改检测。

  tx.executeSql("SELECT (name) FROM note", [], function(tx,res){
      for(var iii = 0; iii < res.rows.length; iii++)
      {
          $scope.courseDB.push(res.rows.item(iii).name);
          alert($scope.courseDB);
      }
      $scope.$apply();
  });