键值对中的 ng-options orderBy 值

ng-options orderBy value in key value pairs

我无法按照 字母顺序 value 的键值对进行排序。

我使用的样本是,

"week_days_short":[
    {"key":"1","value":"Mon"},
    {"key":"2","value":"Tue"},
    {"key":"4","value":"Thu"},
    {"key":"3","value":"Wed"},
    {"key":"5","value":"Fri"},
    {"key":"6","value":"Sat"},
    {"key":"7","value":"Sun"}
]

我用过下面的代码,

ng-options="key as value for (key, value) in 
            properties.week_days_short | 
            orderBy:'value' track by value"

但是排序没有发生。有什么建议吗?

我觉得可以帮到你。

ng-options="key as value.sort((a, b) => a.key.localeCompare(b.key)) for (key, value) in 
        properties.week_days_short | 
        orderBy:'value' track by value"

这是参考:In ES6/ES2015 or later you can do this way

angular.module("app",[])
.controller("ctrl", function($scope) {

$scope.properties = {
  "week_days_short":[
    {"key":"1","value":"Mon"},
    {"key":"2","value":"Tue"},
    {"key":"4","value":"Thu"},
    {"key":"3","value":"Wed"},
    {"key":"5","value":"Fri"},
    {"key":"6","value":"Sat"},
    {"key":"7","value":"Sun"}
  ]};

})
 <script src="//unpkg.com/angular/angular.js"></script>
 <body ng-app="app" ng-controller="ctrl">
    selection={{selection}}<br>
    Select with Alphabetical Order<br>
    <select ng-model="selection"
            ng-options="item.key as item.value for item in 
              properties.week_days_short | 
              orderBy:'value' track by item.value">
      <option value="">Select day</option>
    </select>
  </body>

DEMO on PLNKR

有关详细信息,请参阅

因为 HTML 中的 orderBy 不适用于 Object 而 week_days_short 是数组。所以在控制器上排序解决了我的问题。

Object.values($scope.properties.week_days_short).sort()