问题
So i have got a ng-repeat with some names in it. What i want to do is following. The names that are outputted to the screen are ordered alphabetically.
<div ng-repeat="n in names | orderBy:'name'"></div>
- Anne
- Jane
- John
- Mike
- Ziggy
But is it possible to always show the name 'Mike' as the top name and order the rest alphabetically?
- Mike
- Anne
- Jane
- John
- Ziggy
回答1:
You can write additional sorting function for this. For example:
HTML:
<div ng-repeat="n in names | orderBy:[egoSort, 'name']">{{n}}</div>
Controller:
$scope.egoSort = function(name) {
return name === 'Mike' ? '' : name;
};
This is possible because orderBy
allows you to specify several predicates using array notation.
Demo: http://plnkr.co/edit/FfpcjJBBMm52A71Pa01m?p=preview
回答2:
There is one more way...
add an attribute say order
to the user Object which you want to appear at top.
var specificUserName = {name: Mike,order:0}
//This name would appear first in the list
HTML would be
<div ng-repeat="n in names | orderBy:['order', 'name']">{{n}}</div>
来源:https://stackoverflow.com/questions/25909609/angularjs-orderby