如何瞬间()。来自角度瞬间的诺()?

时间:2017-01-04 20:06:19

标签: javascript angularjs momentjs angular-moment

我怎样才能做到这一刻()。来自角度时刻的fromNow()?

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

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

我可以做到这一点:

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

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

2 个答案:

答案 0 :(得分:2)

您只需使用am-time-ago指令。

这是一个工作示例:

&#13;
&#13;
angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope) {
  $scope.a = {
    Alert: {
      date: new Date()
    }
  };
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

您只需将a.Alert.date设置为日期并在视图中调用fromNow

,就可以执行与Pure JS相同的操作

&#13;
&#13;
angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope, moment) {
  $scope.a = {
    Alert: {
      date: moment()
    }
  };
});
&#13;
<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>
&#13;
&#13;
&#13;