【发布时间】:2016-03-19 14:04:02
【问题描述】:
我已经看到了与此相关的其他问题,并根据我的理解实施了答案。但是,我无法使用 ng-repeat 在视图中显示响应。这是我的代码。
JS 文件:
app.controller('testcontrol', ['$scope', '$http', function($scope, $http) {
$scope.resultset = [];
$http.get('http://localhost:3000/testdata').then(function(data) {
$scope.resultset = data.data.resultset;
console.log($scope.resultset);
},function(error) {
$scope.error = error;
})
}]);
HTML:
<div class="wrapper-md" ng-controller="testcontrol">
<ul>
<li data-ng-repeat="result in resultset">
{{result.name}}
</li>
</ul>
</div>
我可以在控制台中看到数据。但是,我无法在视图中看到这种情况。我不明白为什么不。我正在使用 promise 并初始化变量,这样 ng-repeat 就不会阻塞。
控制台响应:
[Object, Object, Object, Object, Object]
0: Object
avatar: "img/a4.jpg"
followers: "2104"
name: "Chris Fox"
title: "Master Chef"
url: "#"
__proto__: Object
1: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]
【问题讨论】:
-
可能你得到的数据结构与你预期的不同。使用控制台日志更新您的问题。
-
我期望得到的数据结构是一个数组。这不是数据结构的问题。无论如何,我正在使用控制台响应更新问题。
-
我认为在分配后使用 $scope.$apply() 可以解决问题
标签: angularjs ng-repeat angular-http