刷新$ scope更改

赫曼斯

ng-view通过路由交换出来。这是一些路线和控制器代码

var containers = [];
angular.module('AContainer',['ngRoute']).config(
function ($routeProvider){
    $routeProvider
    .when('/', {
        controller: containerListCtrl,
        templateUrl: '/closeAndUplift_containerList.html'
    });
}
);
function containerListCtrl($scope, $location){
    $scope.containers = containers;
}

这是div

<div id='containersDiv' ng-app='AContainer'>
    <div ng-view></div>
</div>

在此视图中,已加载了一个视图(closeAndUplift_containerList.html),该视图通过ng-repeat显示对象数组

<div>
<table>
    <thead>
    <th>Pallet id</th>
    </thead>
    <tr ng-repeat='container in containers'>
        <td><a href='#/edit/{{container.index}}'>{{container.id}}</a></td>
    </tr>
</table>

当jquery ajax回调导致更改时,会发生此问题。xhr调用包含在一个单独的库中,并传入一个回调函数。该回调应清除容器列表。回调执行此操作:

var activeScope = $('#containersDiv').scope();
activeScope.$apply( function(){
    activeScope.containers = [];
});

但是什么也没发生。旧值仍在显示。

编辑:我按照Rishul的建议重新安排了代码,以利用$ http的优势使其工作。问题是,尽管这是一种变通方法,但它并不能真正回答我为什么使用的方法不起作用的问题。有人可以评论是否应该选择他的答案来解决问题吗?

里舒尔·马塔(Rishul Matta)

使用$ http服务进行ajax调用,因为此内置的angular js库将在获取响应时运行摘要循环,并且在您修改范围变量时,视图也会更改。

在您的情况下,jQuery回调不会触发$ digest循环,因此视图不会得到更新!请确认是否有效

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章