角度顺序与日期字符串 [英] Angular orderBy with date string

查看:128
本文介绍了角度顺序与日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照日期(在这种情况下从我的数据库开始'日期)在我的网络应用程序中订购事件。我遇到日期未正确排序的问题。我相信他们是排序字母数字而不是日期,因为我的'开始'字段是一个字符串。我的问题是,如何最佳地将该字段转换为Date,以便正确订购?以下相关代码:

I'm attempting to order events in my web app by date (in this case 'start' date from my db). I'm running into issues where the dates aren't sorted properly. I believe they are sorting alphanumerically rather than by date because my 'start' field is a string. My question is, how to best convert this field to Date for proper ordering? Relevant code below:

html:

<div ng-repeat="event in events | orderBy:'start'">{{event.city}}</div>

角度控制器:

angular.module('MainCtrl', []).controller('MainController', function($scope, $routeParams, Events) {

  // pull empty event details from API based on routeParams to get all events
  $scope.id = '';

  Events.get($scope.id).success(function(response) {
      $scope.events = response;
  });
});

数据库对象示例:

{
  "_id": {
      "$oid": "54978d87ac1e3caaeee8452c"
  },
  "event": "New Years Eve - Times Square",
  "city": "New York",
  "state": "New York",
  "date": "December 31, 2014",
  "start": "December 31, 2014",
  "end": "January 1, 2015",
  "radius": "400",
  "team_1": "",
  "team_2": "",
  "object_id": "10897094",
  "longitude": "-73.9848931",
  "latitude": "40.7591529",
  "photos": []
}


推荐答案

成为一个非常简单的解决方案。我只是在我的 Events.get 中的控制器中添加了以下代码:

Turned out to be a very simple solution. I simply added the following code to my controller inside my Events.get:

for (var i = 0, max = response.length; i < max; i++) {
        $scope.events[i].start = new Date(response[i].start);
}

这篇关于角度顺序与日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆