Skip to content

Instantly share code, notes, and snippets.

@droyad
Created May 12, 2015 07:05
Show Gist options
  • Select an option

  • Save droyad/03b814732f2a169d228a to your computer and use it in GitHub Desktop.

Select an option

Save droyad/03b814732f2a169d228a to your computer and use it in GitHub Desktop.
Humanized Timestamp Directive
export function humanTimestampDirective($interval: ng.IIntervalService): ng.IDirective {
function link(scope, element, attrs) {
var timeoutId;
var date;
function apply() {
var humanised;
if (date) {
humanised = moment(date).fromNow();
} else {
humanised = 'never';
}
element.text(humanised);
}
scope.$watch(attrs.date, newDate => {
date = newDate;
apply();
});
element.on('$destroy', () => $interval.cancel(timeoutId));
// start the UI update process; save the timeoutId for canceling
timeoutId = $interval(apply, 1000);
}
return {
link: link
};
}
.directive('humanTimestamp', ['$interval', humanTimestampDirective])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment