angular-gravatar
angular-gravatar copied to clipboard
Check if Gravatar Returns a 404
Is there a way to check if the gravatar exists, and if it doesn't return some other element?
<img ng-if="gravatarExists" gravatar-src-once="user.email">
<div ng-if"!gravatarExists">Some other element</div>
Any help is appreciated. Thanks in advance!
Hi, that's I good idea. I looks like it's possible if you set the fallback image to '404', because then you can detect if the image was loaded correctly or not by doing a element.bind('error', -> ...)
here:
https://github.com/wallin/angular-gravatar/blob/master/src/angular-gravatar.coffee#L22
However we'd need the variable to be default true
, otherwise it won't even try to load the image.
And, I'd need to figure out in which scope to put the variable if you'd want to use it like in your example.
I'll see if I can get something to work, but unfortunately I don't have so much time to look into it atm. Let me know if you have any ideas, and feel free to do a pull request if you come up with something
Thanks!
I also need a nice way of hiding/showing different elements if the gravatar returns a 404.
Perhaps we could solve it with something like the following:
<img gravatar-error="controller.gravatarExists = false">
element.bind('error', function () {
scope.$apply(attrs[gravatar-error])
})
Thoughts?