使用toISOString()获取ng-repeat中的纪元日期

时间:2015-05-20 16:31:37

标签: javascript jquery angularjs

正如标题所说,我有一张表,其中一列是一个纪元日期。我试图使用'toISOString()`中内置的javascripts来翻译它,但无法弄清楚如何去做。有没有办法在填充表格时动态执行?

以下是我正在使用的js:

angular.module('myapp', [])
  .controller('MainCtrl', function($scope, $http, $filter, $interval) {

    var orderBy = $filter('orderBy');
    $scope.savedOrder = 'name';
    $scope.searchText = ''

    //first http request-----------------------------------
    $http.get('xxxxxxxxxxxxxx').success(function(data) {
      $scope.recentalerts = data;
      $scope.tempdata = data;
      $scope.order('-epochtime');
    });

    $scope.reverse = true;

    //beginning of interval--------------------------------
    $interval(function() {
      $http.get('xxxxxxxxxxxxx').success(function(data) {
        if (!angular.equals(data, $scope.tempdata)) { 
          console.log("here...");

          ...

          $scope.tempdata = data; 

        } //end if

      });
    }, 5000);


    $scope.order = function(predicate) {
      $scope.reverse = !$scope.reverse;
      $scope.recentalerts = orderBy($scope.recentalerts, predicate, $scope.reverse);
      $scope.savedOrder = predicate;
    };

  });

以下是表格主体:

  <tbody>
    <tr data-ng-repeat="alert in recentalerts | orderBy:savedOrder:reverse | filter:searchText">
      <td ng-click="search(alert.epochtime)">{{alert.epochtime}}</td>
      <td ng-click="search(alert.ip)">{{alert.ip}}</td>
      <td ng-click="search(alert.type)">{{alert.type}}</td>
      <td ng-click="search(alert.classification)">{{alert.classification}}</td>
    </tr>
  </tbody>

我尝试了{{alert.epochtime.toISOString()}}但是没有用。我想在我toISOString()之前需要成为日期对象,但有没有办法在ng-repeat内做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用date过滤器

<强> Date Filter Docs

请参阅以下代码。

angular.module('myapp', [])
  .controller('MainCtrl', function($scope) {
    // this will return epoch date
    $scope.epochtime = (new Date).getTime();
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
  <div ng-controller="MainCtrl">
    <div>{{epochtime}}</div>
    <div>{{epochtime | date:'yyyy-MM-dd HH:mm:ss Z'}}</div>
  </div>
</div>