我怎样才能做到这一刻()。来自角度时刻的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"
答案 0 :(得分:2)
您只需使用am-time-ago
指令。
这是一个工作示例:
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;
答案 1 :(得分:-1)
您只需将a.Alert.date
设置为日期并在视图中调用fromNow
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;