我完全糊涂了。当 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>
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 的数据。
Update
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 的 async,所以会是这个原因吗?
Final
我知道为什么。我使用了一个名为jsTree
,这将对节点进行一些装饰,因此我插入的节点将被覆盖。
谢谢你们。