问题描述
我想用使日期看起来更好。问题是,这些日期是从通过动态地REST AngularJS进账。所以,当我附上这个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 - - 它没有依赖关系。
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数据这是角度的方式。而且它是pretty :-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在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!