Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular directive for a fallback image

If an image on a separate server doesn't exist I'd like to display a default image. Is there an angular directive to accomplish this?

like image 954
Matt York Avatar asked May 02 '13 23:05

Matt York


2 Answers

No but you can create one.

http://jsfiddle.net/FdKKf/

HTML:

<img fallback-src="http://google.com/favicon.ico" ng-src="{{image}}"/>

JS:

myApp.directive('fallbackSrc', function () {
  var fallbackSrc = {
    link: function postLink(scope, iElement, iAttrs) {
      iElement.bind('error', function() {
        angular.element(this).attr("src", iAttrs.fallbackSrc);
      });
    }
   }
   return fallbackSrc;
});
like image 107
Ketan Avatar answered Oct 22 '22 00:10

Ketan


Is there an angular directive...

http://ngmodules.org/modules/angular-img-fallback

Github: https://github.com/dcohenb/angular-img-fallback

(32 stars as of now)

like image 33
Mars Robertson Avatar answered Oct 21 '22 22:10

Mars Robertson