AngularJS orderby:将数组元素放在顶部 [英] AngularJS orderby : keep an element of array on top
问题描述
我已经搜索过,但没有找到问题的答案.希望我已经搜索了! 所以,这是我的问题:
I have searched and don't found an answer to my question. Hope, I've well searched ! So, this is my problem :
<div id="{{expression.comment_id.$id}}" class="comments" ng-repeat="expression in expressions| orderBy:orderProp"
因此,orderBy效果很好,但是我有一个特殊的需求: id为'0'的expression.comment_id必须位于最前面.
So, orderBy works great, but I have a specific need : expression.comment_id with '0' id must stay on top.
这怎么办? 我认为我必须使用个人过滤器,但是我不确定这是否是更有效的解决方案. 如果这是个好方法,我该如何正确实施呢?
How can do this ? I think I must use a personnal filter but I'm not sure this is the more efficient solution. If this is a good way, how can I implement it correctly ?
感谢您的帮助!
推荐答案
在这里您可以找到一个小提琴: http://jsfiddle.net/sameersemna/81q9jwg9/
here you go made a fiddle for you: http://jsfiddle.net/sameersemna/81q9jwg9/
基本上,您必须创建自定义orderBy过滤器:
basically you have to make a custom orderBy filter:
<ul ng-repeat="expression in expressions | orderBy:myValueFunction">
在控制器中:
$scope.myValueFunction = function(expression) {
if(expression.comment_id == 0){
return expression.comment_id;
}else{
return expression.orderProp;
}
}
这篇关于AngularJS orderby:将数组元素放在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!