【问题标题】:Issue binding Angular ng-repeat to WebSQL/SQLLite问题将 Angular ng-repeat 绑定到 Web SQL/SQLite
【发布时间】:2015-08-06 22:00:17
【问题描述】:

在 Cordova 应用程序中,我一直在尝试像这样绑定角度标记:

<table class="table table-striped table-responsive">
                            <thead>
                                <tr>
                                    <th>Rotation</th>
                                    <th>ID</th>
                                    <th>Name</th>
                                    <th>DOB</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="item in dtPatients track by item.fldRotation">
                                    <td>{{item.fldRotation}}</td>
                                    <td>{{item.fldID}}</td>
                                    <td>{{item.fldTitle + " " + item.fldForename + " " + item.fldSurname}}</td>
                                    <td>{{item.fldDOB | date:'dd/MMM-yy'}}</td>
                                </tr>
                            </tbody>
                        </table>

像这样在控制器中进行简单的 WebSQL 事务:

tx.executeSql("Select * from [tblPatients];"
                        , []
                        , function (tx, results) {
                            if (results.rows.length == 0) {
                                $scope.dtPatients = null;
                                $scope.$apply();
                            }
                            else {
                                // We have records so we create an array of the results
                                $scope.dtPatients = results.rows;
                                $scope.$apply();
                                debugger;
                            }
                        }
                        , function (tx, err) {
                            console_log("Error retrieving patients.");
                            $scope.dtPatients = null;
                            logdberror(err.code, err.message);
                            errorsource = -1;
                            ShowError('PatientsController1 load patients', err.code.toString + " " + err.message);
                            $scope.$apply();
                        }
                    );

这会产生一个错误,唯一的解决方法是使用here 所示的代码将行转换为数组。我注意到作者在链接中提到的错误,但我不确定这是我得到的错误。我还尝试将 ng-repeat 从上面显示的内容更改回我从一开始就拥有的内容

ng-repeat="item in dtPatients"

无论哪种方式,我都会不断收到相同的错误。绑定之前确定不需要将 SQL 结果集转换为数组吗?

这是给出的错误:

达到 10 次 $digest() 迭代。中止!观察者在最后被解雇 5 次迭代: [[{"msg":"fn: function (c,d){var e=a(c,d);return b(e,c,d)}","newVal":17,"oldVal":15}],[{"msg":"fn: 函数 (c,d){var e=a(c,d);返回 b(e,c,d)}","newVal":19,"oldVal":17}],[{"msg":"fn: 函数 (c,d){var e=a(c,d);返回 b(e,c,d)}","newVal":21,"oldVal":19}],[{"msg":"fn: 函数 (c,d){var e=a(c,d);返回 b(e,c,d)}","newVal":23,"oldVal":21}],[{"msg":"fn: 函数 (c,d){var e=a(c,d);返回 b(e,c,d)}","newVal":25,"oldVal":23}]] at Error (native) at localhost:4400/scripts/angular.min.js:6:417 在 n.$get.n.$digest (localhost:4400/scripts/angular.min.js:124:185) 在 n.$get.n.$apply (localhost:4400/scripts/angular.min.js:126:293)

提前感谢您的任何帮助/建议。

【问题讨论】:

标签: javascript angularjs sqlite cordova web-sql


【解决方案1】:

经过几天的研究,我找到了答案,所以我想我会弹出并发布它,以防其他人想知道如何将 Angular 绑定到 SQLite/WebSQL 结果集。

在上面的问题中,我有一个异步返回结果的回调函数:

else {
    // We have records so we create an array of the results
    $scope.dtPatients = results.rows;
    $scope.$apply();
    debugger;
}

这似乎触发了错误所指的无限循环。我不知道如何或为什么不知道,也许有更好的 Angular 经验的人可以回答这个问题。但是,我找到的解决方案不是像我上面提到的链接那样手动构建一个字符串数组,而是像这样更改代码以一次性完成:

else {
    // We have records so we create an array of the results
    $scope.dtPatients = JSON.parse(JSON.stringify(results.rows));
    $scope.$apply();
    debugger;
}

基本上是“字符串”,然后将其转换回 JSON 对象。我希望我知道为什么你不能只绑定到 SQLite 结果集,但现在这是我能得到的下一个最佳修复/解决方法,而且我知道无论如何这是正确的方法。

【讨论】:

  • 你也可以使用return angular.extend({}, results.rows);
猜你喜欢
  • 2014-07-12
  • 2017-11-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
相关资源
最近更新 更多