Mel*_*kor 5 javascript ajax angularjs angularjs-scope angularjs-ng-repeat
我完全糊涂了.当ajax调用改变其值时,为什么我的ng-repeat不刷新?我在这里看过很多问答,但他们都没有谈到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>
Run Code Online (Sandbox Code Playgroud)
JS:
function EntryCtrl ($scope, $http) {
$scope.rootEntry = [];
$http.get('read/root').success(function(root) {
$scope.rootEntry = root;
console.log($scope.rootEntry);
});
}
Run Code Online (Sandbox Code Playgroud)
控制台确实记录了服务器返回的数组.但是html中的列表从未更新过.如果我使用$scope.$apply,$digest会发生错误.
控制台输出
[Object]
0: Object
Attributes: null
Id: "534580464aac494e24000001"
Name: "Registry"
Path: ""
__proto__: Object
length: 1
__proto__: Array[0]
Run Code Online (Sandbox Code Playgroud)
所以这是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);
}
Run Code Online (Sandbox Code Playgroud)
后者的输出是[].这可能是因为$ 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);
});
}
Run Code Online (Sandbox Code Playgroud)
实例:http ://plnkr.co/edit/Grlgfob6tjE63JizWdCD?p=preview