【问题标题】:updating ng-repeat list on adding data locally in angularjs在 angularjs 中本地添加数据时更新 ng-repeat 列表
【发布时间】:2014-07-24 13:37:55
【问题描述】:

我有一个表格,显示来自 websql 本地数据库的数据列表。单击添加按钮列表医生提供添加到数据库。添加完成但问题是我无法更新数据列表和查看添加的记录。刷新页面后我可以看到添加的记录。任何人都可以在这里帮助我。我在下面发布我的代码。html视图模板如下:

<table  class="table table-hover" >
<tr>
  <th>DOCTOR</th>
  <th>SPECIALITY</th>
  <th>PATCH</th>
  <th>CLASS</th>
  <th> </th>
</tr>
<tr ng-repeat="doc in listDoctors">
  <td>{{ doc.contactName }}</td>
  <td>{{ doc.speciality }}</td>
  <td>{{ doc.townName }}</td>
  <td>{{ doc.class }}</td></tr></table>  

现在控制器可以:

 //displays list of doctors
        $scope.listDoctors = [];
           readData.transaction(sqlDoctorDetails,0)
         .then(function (data) {
           $scope.listDoctors=data;
          });

将数据添加到数据库

 $scope.AddToList = function() {
  var db = openDatabase("database");
    db.transaction(function(tx) {
    tx.executeSql(insertStatement);
  });  

请帮助我改进代码以更新在数据库中添加新条目的列表。 sqlDoctorDetails 和 insertStatement 是我的查询变量,分别用于显示和插入数据。

【问题讨论】:

  • 您需要将医生添加到 $scope.listDoctors 数组中并保存到数据库中

标签: angularjs


【解决方案1】:

在 AddToList 函数中推送最新的医生详细信息,这也会更新列表

所以每当你点击添加按钮并调用控制器函数时:

$scope.addDoctorToDB(doctorDetail){
     //add it to the doctor's list
     $scope.listDoctors.push(doctorDetail);
     //Now do the db operation say calling your add function
     this.AddToList();
}

【讨论】:

    【解决方案2】:

    我的 Angular 知识已生疏,但我认为在插入数据库后,您需要插入内存模型 listDoctors 或触发 sqlDoctorDetails 查询以将更新的数据读入 listDoctors

    Angular 会观察模型的变化,但不会观察模型背后的数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多