I'm not sure that scope.$watch does what you are expecting it to do; scope.$watch takes as its first argument an expression to evaluate on the current scope; when that expression returns a new value, it will call the second argument, a function, with the new value. Thus,
scope.$watch('ready', function() {...});
is basically the same as saying
Call this function every time scope.ready changes.
which is obviously not what you want.
On to your functionality--there are a few ways you might go about implementing something like this. The first is a simple filter:
app.filter('timeago', function() {
return function(time) {
if(time) return jQuery.timeago(time);
else return "";
};
});
<p>The timestapm was {{conversation.timestamp|timeago}} ago.</p>
In this case, however, the returned string would automatically refresh any time a digest cycle is run on the scope.
To only process the timestamp exactly once, you might use a directive like the following:
app.directive('timeago', function($timeout) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
scope.$watch(attrs.timeago, function(value) {
if(value) elem.text(jQuery.timeago(value));
});
}
};
});
<p>The timestamp was <span timeago="conversation.timestamp"></span> ago.</p>
Here is a version that re-runs a digest cycle every 15 seconds, to automatically update the timestamp every so often:
app.directive('timeago', function($timeout) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
var updateTime = function() {
if (attrs.timeagoAutoupdate) {
var time = scope.$eval(attrs.timeagoAutoupdate);
elem.text(jQuery.timeago(time));
$timeout(updateTime, 15000);
}
};
scope.$watch(attrs.timeago, updateTime);
}
};
});
<p>The timestamp was <span timeago="conversation.timestamp"></span> ago.</p>
Here is a jsFiddle that demonstrates all three examples. Do note that the only reason the third example (with the filter) is automatically updating every minute is becaues the second example (the timeagoAutoupdate directive) is calling scope.$eval.