按日期重复排序并不起作用

时间:2016-06-20 11:50:49

标签: angularjs

为什么以下代码没有按日期排序?

http://jsfiddle.net/eliaweiss/1cs3xbtd/1/

HTML

<div ng-app>
    <script type="text/javascript" src="http://code.angularjs.org/1.0.1/angular-1.0.1.js"></script>
    <div ng:controller="Main">
      <div ng:repeat="(id, i) in items | orderBy:'date'">{{id}}: {{i.date | date}}</div>
    </div>
</div>

JS

function Main($scope) {
    $scope.items = {
        0: {date: new Date('12/23/2013')},
        1: {date: new Date('12/23/2011')},
        2: {date: new Date('12/23/2010')},
        3: {date: new Date('12/23/2015')}
    };
}

2 个答案:

答案 0 :(得分:3)

问题非常简单.... javascript对象没有排序,因此orderBy仅适用于数组

将模型结构更改为数组

答案 1 :(得分:0)

charlietfl说:

JS:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.items = [
    {
        id: 0,
        date: new Date('12/23/2013')
    },
    {
        id: 1,
        date: new Date('12/23/2011')
    },
    {
        id: 2,
        date: new Date('12/23/2010')
    },
    {
        id: 3,
        date: new Date('12/23/2015')
    }];
})

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng:repeat="i in items | orderBy:'date'">{{i.id}}: {{i.date | date}}</div>
</div>