如何在select指令的弹出选项窗口中更改项目的顺序?

时间:2016-07-05 09:46:11

标签: angularjs

我在项目中使用了angularJS select指令。

以下是示例:

模板:

<div ng-app>
  <div ng-controller="Ctrl">
    <select ng-model="selected" ng-options="f.name for f in friends "></select>      
  </div>
</div>

控制器:

function Ctrl($scope) {
  $scope.friends =
      [{name:'John', phone:'555-1212', age:0},
       {name:'Mary', phone:'555-9876', age:0},
       {name:'Mike', phone:'555-4321', age:0},
       {name:'AllNames', phone:'5558-55', age:0},
       {name:'Adam', phone:'555-5678', age:0},
       {name:'Julie', phone:'555-8765', age:0}];

       $scope.selected = $scope.friends[3];
}

以下是fiddle

The  `friends` list is list that I get from database table and it ordered as list above.

当我点击select指令时,会弹出窗口选项,其中包含可以选择的所有选项。我需要在选项窗口的列表顶部显示 AllNames 项。< / p>

知道如何实施它?

1 个答案:

答案 0 :(得分:0)

只需将ng-options更改为:

ng-options="f.name for f in friends | orderBy: 'age' "

修改:工作fiddle