javascript - Ng-Repeat 在资源查询后不更新 - $apply() 不起作用

标签 javascript angularjs resources angularjs-ng-repeat

我在使用 ng-repeat 更新 View 时遇到问题,该 View 在使用查询从服务器加载数据后不会刷新。

这是我的代码:

Controller :

vm.getPage = function() { 
            rendezvousResource.query(vm.gridOptions, function(result) {
                vm.gridData.totalItems = result.total;
                vm.gridData.totalPages = result.last_page;
                vm.gridOptions.page = result.current_page;
                vm.gridOptions.data = result.data;
                vm.rendezvouss = result.data;
                // $scope.$apply();
            });

        };

查看:

        <table ng-controller="RendezvousListCtrl as vm"  class="table table-hover">
            <thead>
                <tr>
                    <th>Client</th>
                    <th>Date Début</th>
                    <th>Date Fin</th> 
                    <th>Type RDV</th>
                    <th>Status</th>
                    <th>Notification</th>
                </tr>
            </thead>
            <tbody>  
                <tr ng-repeat="rendezvous in vm.rendezvouss">
                    <td>{{ rendezvous.nomClient }}</td> 
                    <td>{{ rendezvous.debut }}</td>
                    <td>{{ rendezvous.fin }}</td>
                    <td>{{ rendezvous.typerdv }}</td>
                    <td>{{ rendezvous.status }}</td> 
                    <td>{{ rendezvous.notification }}</td> 
                </tr>
            </tbody>
        </table>

我已经尝试添加 $scope.$apply() 但我得到一个:Error: error:inprog 行动已在进行中

我已经在回调函数内尝试了 $timeout 但它不起作用,在 getPage 上应用它的函数之外,它说 undefined 不是一个函数。

我也尝试使用 .then 和 $promise.then 但它的答案也是 undefined is not a function。

请问有什么帮助吗? :)

最佳答案

我的 Controller 已实例化两次(我的错误),但这就是导致 ngRepeat 不刷新的原因。

为了能够看到这一点,我安装了 AngularJS Batarang chrome extension 。它提供了清晰的范围 View ,并且能够看到我的 Controller 已实例化两次。

关于javascript - Ng-Repeat 在资源查询后不更新 - $apply() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28036481/

相关文章:

javascript - 在 Angular 2+ 中使用 Typescript/JavaScript 从对象中删除重复字段

javascript - 解析分组的json

jsf - 排除 JSF 中的资源

amazon-web-services - DynamoDB - 是否需要调用 shutdown()?

java - 写入资源文件java?

javascript - 无法将服务注入(inject)其 Angular 单元测试

javascript - Angular 组件每 X 秒刷新一次页面数据

javascript - 如何在 React 中访问 DOM 元素? React 中 document.getElementById() 的等价物是什么

javascript - HTML 和 Angular 中的动态变量?

javascript - Angular UI-Router 认为每个 URL 都不匹配并重定向