加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:55:47 所属栏目:百科 来源:网络整理
导读:我完全糊涂了.当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 i
我完全糊涂了.当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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读