一起使用 jQuery timeago 或 momentjs 和 AngularJS [英] Use jQuery timeago or momentjs and AngularJS together
问题描述
我想使用 timeago 插件 使日期看起来更好.问题是这些日期是通过 AngularJS 从 REST 动态获取的.因此,当我将此 jQuery 插件附加到我的页面时,它只是不处理它.
I want to use timeago plugin to make dates look nicer. The problem is that these dates are fetched via AngularJS from the REST dynamically. So, when I attach this jQuery plugin to my page, it just doesn't process it.
那么,如何更好地做这些事情呢?如果可能的话,我很乐意在没有 jQuery 的情况下运行.
So, how to better do such things? I would be happy to run without jQuery at all if possible.
推荐答案
我会使用 momentjs - http://momentjs.com/ - 它没有依赖项.
I would use momentjs - http://momentjs.com/ - it has no dependencies.
然后您可以创建一个名为timeAgo"或fromNow"的过滤器.您可能应该将其称为 fromNow
因为这就是 momentjs 所称的:
Then you can just create a filter called 'timeAgo' or 'fromNow'. You should probably call it fromNow
because that's what momentjs calls it:
angular.module('myApp').filter('fromNow', function() {
return function(date) {
return moment(date).fromNow();
}
});
然后您只需在视图中使用简单的数据绑定:
Then you would just use simple data binding in your view:
<p>Pizza arrives {{pizzaArrivalDate | fromNow}}</p>
如果你真的想使用 jQuery 插件,你可能需要编写一个指令.但这种做法很糟糕,因为它将您的数据链接到一个 DOM 元素.我上面的方式将数据与 DOM 分开,这是一种角度方式.它很漂亮:-D
If you really wanted to use the jQuery plugin, you would probably have to write a directive. But that way of doing it is bad because it links your data to a DOM element. The way I put above seperates data from DOM which is the angular way. And it's pretty :-D
这篇关于一起使用 jQuery timeago 或 momentjs 和 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!