如何在 angular 时刻 moment().fromNow() ?

how to moment().fromNow() in angular-moment?

如何在 angular 时刻完成这个时刻 ().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

您可以简单地使用 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>

您可以通过简单地将 a.Alert.date 设置为时刻日期并在视图中调用 fromNow 来完成与在 Pure JS 中所做的相同的事情

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>