订单不在AngularJS中工作

时间:2016-03-10 10:13:36

标签: angularjs

我正在使用orderBy订购下拉列表,但它无效。有人可以就此提出建议吗?如果您需要任何其他信息,请告诉我。

<select class="selectbox form-control" class="form-control" data-ng-model="ctrl.task.delegateFromId" data-ng-options="user.marsId as user.fullName for user in ctrl.userList | orderBy:'fullName' ">
   <option value="">-- Select Area Owner --</option>
</select>

here is plunker for this question 1st drop down order by is working but for 2nd it is not working

2 个答案:

答案 0 :(得分:1)

我不知道你的问题是什么,但我举了一个简单的例子。

var myapp = angular.module('myapp', []);
myapp.controller('Ctrl', function ($scope) {
  var vm = this;
    vm.userList =JSON.parse('{"11":{"marsId":"11","firstName":"cc","lastName":"cc","fullName":"cc cc"},"12":{"marsId":"12","firstName":"aa","lastName":"aa","fullName":"aa aa"},"45":{"marsId":"45","firstName":"kk","lastName":"kk","fullName":"kk kk"}}');
  
  
  vm.userList2 = [];
for (var key in vm.userList) {
  var tempObj = "";
  tempObj = vm.userList[key];
  vm.userList2.push(tempObj);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="Ctrl as vm">
        <select class="selectbox form-control" class="form-control" data-ng-model="vm.task.delegateFromId" data-ng-options="user.marsId as user.fullName for user in vm.userList2 | orderBy:'fullName' ">
   <option value="">-- Select Area Owner --</option>
          
</select>
</div>

答案 1 :(得分:0)

亲爱的,如果您希望按字母顺序对名称进行排序,请尝试以下操作:

<select class="selectbox form-control" class="form-control" data-ng-model="ctrl.task.delegateFromId" data-ng-options="user.marsId as user.fullName for user in ctrl.userList | orderBy:'toString()' ">
   <option value="">-- Select Area Owner --</option>
</select>
  1. 您需要添加ng-model以正确使绑定适用于字符串列表。
  2. 如果输入包含字符串列表,您可以使用toString()进行排序。由于orderBy的表达式可以是Getter函数。该函数的结果将使用&lt;,=,&gt;进行排序。操作者。
  3. 谢谢&amp;干杯