Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery timeago or momentjs and AngularJS together

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.

So, how to better do such things? I would be happy to run without jQuery at all if possible.

like image 233
Sergei Basharov Avatar asked Feb 08 '13 14:02

Sergei Basharov


1 Answers

I would use momentjs - http://momentjs.com/ - it has no dependencies.

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> 

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

like image 136
Andrew Joslin Avatar answered Oct 13 '22 21:10

Andrew Joslin