我知道这可能是我的错误。
我收到一个包含 WordPress 帖子的 JSON 对象,但是当我尝试在 DOM 中渲染这些帖子时,列表中出现 12 次的唯一帖子是最后一项
<div ng-repeat="post in posts">
<h2 ng-bind-html="post.title"></h2>
<p>{{:: post.date | date}}</p>
</div>
Controller
$scope.posts = [];
$scope.doRefresh = function() {
$scope.posts = FreshlyPressed.getBlogs($scope);
$scope.$broadcast('scroll.refreshComplete');
}
$scope.doRefresh();
服务
.service('FreshlyPressed', function($http, $q) {
return {
getBlogs: function($scope) {
var posts = [];
$http.get('http://urbanetradio.com/wp-json/posts')
.success(function(result) {
_.each(result, function(posts) {
console.log(posts.title);
$scope.posts = posts;
});
})
}
});
最佳答案
_.each(result, function(posts) {
console.log(posts.title);
$scope.posts = posts;
});
这就是错误所在。 Lodash 循环遍历帖子,并每次将 $scope.posts 分配给一篇帖子,这就是为什么你会得到最后一篇。
尝试:
$http.get('http://urbanetradio.com/wp-json/posts')
.success(function(result) {
$scope.posts = result;
})
}
关于javascript - ng-repeat 仅显示最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182773/