微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当ajax调用更改其值时,不会更新AngularJS中的javascript-ng-repeat列表

我完全糊涂了.当ajax调用改变其值时,为什么我的ng-repeat不刷新?我见过很多Q&在这里,但他们都没有谈到ajax电话.

HTML

<div class="row" ng-controller="EntryCtrl">
    <div id="treeview" class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
        <ul>
            <li ng-repeat="root in rootEntry">{{root.Name}}</li>
        </ul>
    </div>
</div>

JS:

function EntryCtrl ($scope,$http) {
    $scope.rootEntry = [];
    $http.get('read/root').success(function(root) {
        $scope.rootEntry = root;
        console.log($scope.rootEntry);
    });
}

控制台确实记录了服务器返回的数组.但是html中的列表从未更新过.如果我使用$scope.$apply,则会出现关于$digest的错误.

控制台输出

[Object]
    0: Object
        Attributes: null
        Id: "534580464aac494e24000001"
        Name: "Registry"
        Path: ""
        __proto__: Object
        length: 1
    __proto__: Array[0]

所以这是服务器返回的root结构.这是数据类型的问题吗?因为它是Object而不是Array.我使用Golang作为服务器并使用json.Marshal()来打包MongoDB中的数据.

更新

function EntryCtrl ($scope,$http) {
    $scope.rootEntry = [];
    $http.get('read/root').success(function(root) {
        $scope.rootEntry = root;
        console.log($scope.rootEntry);
    });
    console.log($scope.rootEntry);
}

后者的输出是[].这可能是因为$http的异步,所以它的原因是什么?

最后

我知道为什么.我使用了一个名为jsTree的插件,它将对节点进行一些装饰,因此我插入的节点将被覆盖.

谢谢你们.

解决方法

我已经测试了你的代码,并且正如tymeJV所说,你的实现没有问题,我建议检查控制台窗口是否存在一些可能的错误.

例:

function EntryCtrl ($scope,$http) {
    $scope.rootEntry = [];
    $http.get('https://api.github.com/users/mralexgray/repos').success(function(root) {
        $scope.rootEntry = root;
        console.log($scope.rootEntry);
    });
}

实例:http://plnkr.co/edit/Grlgfob6tjE63JizWdCD?p=preview

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐