I would like to have AngularJS filter timeAgo that should be applied for a timestamp to show time passed from timestamp. That's an easy one:
filter('timeago', function() {
return function(input) {
return Date.now() - input;
}
});
But when I wanted to make it LIVE(when user on the page - we update the value each second/each digest) it appeared to be not that easy. We don't change input value, so filter is not recalculated. Is it possible to make it recalculable each second without adding extra parameters?
Fiddle example.
It happens because the filters are now stateless, to make it work for this filter you will need to do:
function timeagoFilter(input) {
return Date.now() - input;
}
timeagoFilter.$stateful = true;
return timeagoFilter;
but personally I'd turn it into a directive and change the value with plain javascript - if it's just for display purposes - since it's easy to mess up digest cycle and make the app performance degrade