Created
May 12, 2015 07:05
-
-
Save droyad/03b814732f2a169d228a to your computer and use it in GitHub Desktop.
Humanized Timestamp Directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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