AngularJS - orderBy 投票计数
AngularJS - orderBy vote count
<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
时,我得到了针对该特定 post 的投票(喜欢)数量,我试图将它们从最多到最少排序。
在我的控制器中,我只是检索所有 posts 并将它们返回到部分:
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}}
,它确实正确地计算了金额。
投票是 post 中的一个数组,其中包含所有已完成投票的 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
的 post 有 2 'likes' 票。
举个例子: 如果所有其他 post 都投了 1 票,但这个投了 2 票,我想把这张票显示在最前面。
<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">
如果我理解正确的话这可能有效
你只是在你的 ng-repeat 中放置了下面的过滤器
ng-repeat="post in posts | orderBy : 'votes.length' : true | limitTo:10"
而不是
ng-repeat="post in posts | orderBy : post.votes.length | limitTo:10"
<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
时,我得到了针对该特定 post 的投票(喜欢)数量,我试图将它们从最多到最少排序。
在我的控制器中,我只是检索所有 posts 并将它们返回到部分:
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}}
,它确实正确地计算了金额。
投票是 post 中的一个数组,其中包含所有已完成投票的 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
的 post 有 2 'likes' 票。
举个例子: 如果所有其他 post 都投了 1 票,但这个投了 2 票,我想把这张票显示在最前面。
<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">
如果我理解正确的话这可能有效
你只是在你的 ng-repeat 中放置了下面的过滤器
ng-repeat="post in posts | orderBy : 'votes.length' : true | limitTo:10"
而不是
ng-repeat="post in posts | orderBy : post.votes.length | limitTo:10"