如何通过键作为整数订购?
我有以下对象;
$scope.data = {
"0": { data: "ZERO" },
"1": { data: "ONE" },
"2": { data: "TWO" },
"3": { data: "TREE" },
"5": { data: "FIVE" },
"6": { data: "SIX" },
"10":{ data: "TEN" },
"11": { data: "ELEVEN" },
"12": { data: "TWELVE" },
"13": { data: "THIRTEEN" },
"20": { data: "TWENTY"}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div ng-repeat="(key,value) in data">
Run Code Online (Sandbox Code Playgroud)
当前的输出顺序是 1,10,11,12,13,14,2,20,3,4,5,6
但我想要 1,2,3,4,5,6,10,11,12,13,14,20
| orderBy:key
Run Code Online (Sandbox Code Playgroud)
不适合我.
有任何想法吗?
谢谢!
rnr*_*ies 19
一个选项是使用中间过滤器.
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope) {
$scope.template = {
"0": { data: "ZERO" },
"1": { data: "ONE" },
"2": { data: "TWO" },
"3": { data: "TREE" },
"5": { data: "FIVE" },
"6": { data: "SIX" },
"10":{ data: "TEN" },
"11": { data: "ELEVEN" },
"12": { data: "TWELVE" },
"13": { data: "THIRTEEN" },
"20": { data: "TWENTY"}
}
});
app.filter('toArray', function() { return function(obj) {
if (!(obj instanceof Object)) return obj;
return _.map(obj, function(val, key) {
return Object.defineProperty(val, '$key', {__proto__: null, value: key});
});
}});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore.js"></script>
<body ng-app="app" ng-controller="MainCtrl">
<div ng-repeat="(key, value) in template| toArray | orderBy:key">{{key}} : {{value.$key}} : {{value.data}}</div>
<body>Run Code Online (Sandbox Code Playgroud)
上面的过滤器需要Underscore.js,如果你不使用它,可以重写过滤器.
| 归档时间: |
|
| 查看次数: |
31466 次 |
| 最近记录: |