Create a component that shows the placeholder image before loading the downloaded image, and hides the requested image. After loading the image, you will hide the placeholder and show the image.
@Component({ selector: 'image-loader', template: `<img *ngIf="!loaded" src="url-to-your-placeholder"/> <img [hidden]="!loaded" (load)="loaded = true" [src]="src"/>` }) export class ImageLoader { @Input() src; }
See how Plunker works.
Update
Now that I have a better understanding of the requirements, here is a solution with a background image. It's a bit hacky and I like how the original is better ...
@Directive({ selector: '[imageLoader]' }) export class ImageLoader { @Input() imageLoader; constructor(private el:ElementRef) { this.el = el.nativeElement; this.el.style.backgroundImage = "url(http://smallenvelop.com/demo/image-loading/spinner.gif)"; } ngOnInit() { let image = new Image(); image.addEventListener('load', () => { this.el.style.backgroundImage = `url(${this.imageLoader})`; }); image.src = this.imageLoader; } }
Updated plunker .
source share