刷新$scope更改

Refresh $scope changes

本文关键字:更改 scope 刷新      更新时间:2023-09-26

通过路由交换出ng视图。下面是一些路由和控制器代码

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使其工作。问题是,虽然这是一种变通方法,但它并没有真正回答为什么我使用的方法不起作用的问题。有人能评论一下他的答案是否可以作为解决问题的答案吗?

使用一个$http服务来进行ajax调用,因为这个内置的angular js库在获得响应时会运行一个摘要循环,并且当你修改作用域变量时,视图也会发生变化。

在你的情况下,jquery回调将不会触发$digest周期,因此视图不会得到更新!请确认是否有效