【发布时间】:2016-06-01 13:21:45
【问题描述】:
<a ui-sref="detail({id: post._id})" ng-if="post" ng-repeat="post in posts |
orderBy : post.votes.length | limitTo:10" class="list-group-item">
<div class="row">
{{post.votes.length}}
<div class="col-md-12">
<h5 class="no-margin-top"><span class="glyphicon glyphicon-{{post.type}}-sign"></span> <strong>{{post.birdname}}</strong></h5>
</div>
<div class="col-md-4">
<div class="thumbnail no-border no-margin-bottom">
<img ng-src="{{post.picture}}" alt="bird"/>
</div>
</div>
<div class="col-md-6">
<h5> <span class="fa fa-map-marker"></span> {{post.place}}</h5>
<h5> <i class="fa fa-user"></i> {{post.author}}</h5>
<h5> <i class="fa fa-clock-o"></i> <span am-time-ago="post.created_at"></span></h5>
</div>
</div>
<hr />
</a>
在尝试post.votes.length 时,我得到了对该特定帖子的投票数(点赞数),并且我尝试从最多到最少对它们进行排序。
在我的控制器中,我只是检索所有帖子并将它们返回到部分:
app.controller('dashboardCtrl', ['$scope', '$http', 'postApi', function($scope, $http, postApi){
$scope.posts = [];
$scope.pageSize = 4;
$scope.currentPage = 1;
postApi.getAllPosts()
.then(function(res) {
$scope.posts = res.data.filter(function(posts) {
return posts;
});
});
}]);
但是 ng-repeat 不按最高值排序。只是为了确保它传递了金额,我在下面做了一个{{post.votes.length}},它确实计算了金额。
投票是帖子内的一个数组,其中包含所有已完成投票的 id(用引用链接),如下所示:
{
"_id": "574ddaab594d3e6514ddf6e7",
"created_at": "2016-05-31T18:40:43.727Z",
"author": "Author",
"userId": "154815165165",
"body": "body",
"type": "ok",
"__v": 32,
"votes": [
"574ede99277c97a810379328",
"574ee07b277c97a81037932a"
],
"comments": []
}
因此,您可以在上面看到 ID 为 574ddaab594d3e6514ddf6e7 的帖子有 2 个“喜欢”,即投票。
让我们以此为例:如果所有其他帖子都有 1 票,但这个有 2 票,我想在顶部显示此票。
【问题讨论】:
-
尝试引用 post.votes.length:
ng-repeat="post in posts | orderBy : 'post.votes.length' | limitTo:10" -
@RomanKoliada 我已经试过了,也不管用
-
ng-repeat="post in posts | orderBy : 'votes.length' | limitTo:10"应该可以工作。 -
我不太确定,但有时空格会导致错误。你试过像
orderBy:'post.votes.length'这样写吗? -
刚试了一下,结果一样。
标签: angularjs angularjs-orderby variable-length