Last active
December 16, 2015 17:19
-
-
Save JensRantil/5470122 to your computer and use it in GitHub Desktop.
Improvement of http://stackoverflow.com/a/14774995/260805 that handles generated title attribute on timeago tags.
This file contains 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
<html ng-app="MyApplication"> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> | |
<script src="timeago-angular.js" type="text/javascript"></script> | |
<script src="my-app.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<div ng-controller="myController"> | |
<span class="time-ago" title="{{ myTime }}"></span> | |
</div> | |
</body> | |
</html> |
This file contains 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
angular.module('MyApplication', ['timeAgo']) | |
.controller('myController', function($scope) { | |
$scope.myTime = "2008-07-17T09:24:17Z"; | |
}); |
This file contains 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
angular.module('timeAgo', []) | |
.directive("timeAgo", function($q) { | |
return { | |
restrict: "C", | |
scope: { | |
title: '@' | |
}, | |
link: function(scope, element, attrs) { | |
// Using deferred to assert we only initialize timeago() once per | |
// directive. | |
var parsedDate = $q.defer(); | |
parsedDate.promise.then(function() { | |
jQuery(element).timeago(); | |
}); | |
attrs.$observe('title', function(newValue) { | |
parsedDate.resolve(newValue); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful directive. Thank you!