Angular directive for a fallback image

AngularjsAngularjs Directive

Angularjs Problem Overview


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?

Angularjs Solutions


Solution 1 - Angularjs

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;
});

Solution 2 - Angularjs

> 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)

Solution 3 - Angularjs

Angualr 2 Version

https://github.com/VadimDez/ng2-img-fallback

HTML

<img fallback-src="http://google.com/favicon.ico" src="http://google.com/failedImage.png"/>

Angular 2 Component

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
  selector: '[fallback-src]'
})
export class FallbackSrc {

  @Input('fallback-src') imgSrc: string;
  private el: HTMLElement;
  private isApplied: boolean = false;
  private EVENT_TYPE: string = 'error';

  constructor(el: ElementRef) {
    this.el = el.nativeElement;
    this.el.addEventListener(this.EVENT_TYPE, this.onError.bind(this))
  }

  private onError() {
    this.removeEvents();

    if (!this.isApplied) {
      this.isApplied = true;
      this.el.setAttribute('src', this.imgSrc);
    }
  }

  private removeEvents() {
    this.el.removeEventListener(this.EVENT_TYPE, this.onError);
  }

  ngOnDestroy() {
    this.removeEvents();
  }
}

Solution 4 - Angularjs

I wrote my own fallback lib.

A pretty simple and straightforward angular fallback image lib:

https://github.com/alvarojoao/angular-image-fallback

Utility to work with loading images and handling image error, has image-holder to handle errors in image loading and image-loading for images loading placeholders

http://alvarojoao.github.io/angular-image-fallback

##Usage

Just add the image attribute to your <img /> tags

<img image="{{'path/to/img.jpg'}}" />

Make sure you don't use ng-src as your image src attribute.

##Advanced options

###with custom fallback and loading placeholders:

<img image="{{image.url}}" image-loading="/image/loading.gif" 
     image-holder="/image/error.png" />

##Example:

https://jsfiddle.net/alvarojoao/4wec4gsq/embedded/result/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt YorkView Question on Stackoverflow
Solution 1 - AngularjsKetanView Answer on Stackoverflow
Solution 2 - AngularjsMars RobertsonView Answer on Stackoverflow
Solution 3 - AngularjsMichael WarnerView Answer on Stackoverflow
Solution 4 - AngularjsAlvaro JoaoView Answer on Stackoverflow