Skip to content

Instantly share code, notes, and snippets.

@MattDionis
Last active August 29, 2015 14:20
Show Gist options
  • Save MattDionis/d91f367150b32453295b to your computer and use it in GitHub Desktop.
Save MattDionis/d91f367150b32453295b to your computer and use it in GitHub Desktop.
Angular directive to replace '404' image with default image
angular
.module('app')
.directive('defaultImage', defaultImage);
function defaultImage() {
var directive = {
link: link,
restrict: 'A'
};
return directive;
function link(scope, element, attrs) {
element.bind('error', function() {
element.attr('src', attrs.defaultImage);
})
}
}
<img ng-src="{{photo.profile}}" default-image="https://instagramimages-a.akamaihd.net/profiles/anonymousUser.jpg">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment