Created
December 4, 2014 19:51
-
-
Save rachaelshaw/59cba66007a7fd289490 to your computer and use it in GitHub Desktop.
Gravatar Angular 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
/** | |
* gravatar | |
* ------------------------------------------------------------------------ | |
* This is a custom directive for using gravatars. | |
* | |
* Usage: | |
* ``` | |
* <gravatar email="[email protected]" | |
* default-img="http://placecage.com/c/200/200"></gravatar> | |
* ``` | |
* | |
* | |
* NOTES: | |
* | |
* This directive needs md5.js in order to convert an email into a gravatar url. | |
* (https://github.com/blueimp/JavaScript-MD5/blob/master/js/md5.min.js) | |
* | |
* Also, f you want to use gravatar's default image instead of | |
* a custom default image, replace the template with: | |
* ``` | |
* '<img src="http://gravatar.com/avatar/{{gravatarHash}}"/>' | |
* ``` | |
*/ | |
angular.module('MyApp') | |
.directive('gravatar', function () { | |
return { | |
restrict: 'E', | |
template: '<img src="http://gravatar.com/avatar/{{gravatarHash}}?d={{defaultImgUrlEncoded}}"/>', | |
scope: { | |
email: '@', | |
defaultImg: '@' | |
}, | |
link: function(scope) { | |
scope.gravatarHash = md5(scope.email); | |
scope.defaultImgUrlEncoded = encodeURIComponent(scope.defaultImg); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment