2

我怎样才能在角矩中做这个 moment().fromNow() ?

<div class="date pull-right"> {{ a.Alert.date }}...</div>

如何将 fromNow() 应用于我的变量“a.Alert.date”?

在 momentjs 我可以这样做:

moment().fromNow()
"a few seconds ago"
moment('2016-12-11').fromNow()
"23 days ago"

https://github.com/urish/angular-moment

4

2 回答 2

3

您可以简单地使用am-time-ago指令。

这是一个工作示例:

angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope) {
  $scope.a = {
    Alert: {
      date: new Date()
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.min.js"></script>

<div ng-app="MyApp" ng-controller="AppCtrl">
  <span am-time-ago="a.Alert.date"></span>
</div>

于 2017-01-04T20:46:46.703 回答
-1

您可以像在 Pure JS 中所做的那样做同样的事情,只需将其设置a.Alert.date为时刻 Date 并fromNow在视图中调用

angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope, moment) {
  $scope.a = {
    Alert: {
      date: moment()
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.min.js"></script>

<div ng-app="MyApp" ng-controller="AppCtrl">
  <div class="date pull-right"> {{ a.Alert.date.fromNow() }}...</div>
</div>

于 2017-01-04T20:54:59.030 回答