在orderBy上排除项目

时间:2014-05-09 06:12:09

标签: javascript angularjs

说我在ng-repeat中有4个项目。如何在orderBy中排除一个项目?

<li ng-repeat="item in items | orderBy:'id':true">

$scope.items = [
    {"name":"item1","id":1},
    {"name":"item2","id":2},
    {"name":"item3","id":3},
    {"name":"item4","id":4}
];

我怎样才能说id:3总是作为第一项出现?

plunker demo

1 个答案:

答案 0 :(得分:9)

您可以创建一个函数来更改按(plunker)排序的值:

$scope.itemSortValue = function(item) {
  if (item.id == 3)
    return -1;
  return item.id;
}

HTML:

<li ng-repeat="item in items | orderBy:itemSortValue">
  {{item.name}}
</li>