Best way to preload images with Angular.js

Yuri

Angular's ng-src keeps previous model until it preloads image internally. I am using different image for the banner on each page, when I switch routes, i change main view, leaving header view as it is, just changing bannerUrl model when I have it.

This is resulting in seeing previous banner image while new one is loading.

I was surprised that there's no directive for it yet, but I wanted to make a discussion before trying to build one.

What I want to do I think is have banner model on custom attribute. like:

<img preload-src="{{bannerUrl}}" ng-src="{{preloadedUrl}}">

Then $scope.watch for bannerUrl change, and as soon as it changes, replace ng-src with loader spinner first, and then create temproary img dom element, preload image from preload-src and then assing it to preloadUrl.

Need to think how to handle multiple images too for galleries for example.

Does anyone have any input on it? or maybe someone can point me to existing code?

I've seen existing code on github that uses background-image - but that doesn't work for me as I need dynamic height/width as my app is responsive, and I cannot do it with background-image.

Thank you

Michal Charemza

Having the 2 urls on the directive seems a touch overcomplicated. What I think is better is to write a directive that works like:

<img ng-src="{{bannerUrl}}" spinner-on-load />

And the directive can watch ng-src and (for example) set visibility:false with a spinner until the image has loaded. So something like:

scope: {
  ngSrc: '='
},
link: function(scope, element) {
  element.on('load', function() {
    // Set visibility: true + remove spinner overlay
  });
  scope.$watch('ngSrc', function() {
    // Set visibility: false + inject temporary spinner overlay
  });
}

This way the element behaves very much like a standard img with an ng-src attribute, just with a bit of extra behaviour bolted on.

http://jsfiddle.net/2CsfZ/47/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fastest way to preload/load large images

From Dev

JS function to preload images before page load

From Dev

Angular JS - Best Way to UI Testing Angular js projects

From Dev

Angular JS - Best Way to UI Testing Angular js projects

From Java

Best way to loop images in react

From Dev

best way to load images of database

From Dev

iOS best way to store images

From Dev

Best way to load Images into a slider

From Dev

best way to present images in SpriteKit

From Dev

the best way to load images into android

From Dev

image preload on angular js routing behaviour

From Dev

Best way for deploy angular.js application made with yeoman?

From Dev

Best way for deploy angular.js application made with yeoman?

From Dev

what is the best way to store data in client side(angular js)?

From Dev

How do I preload images into dropzone.js

From Dev

JS/CSS: Preload images from the HTML file while they are hidden

From Dev

Best way for JS architecture?

From Dev

Is there a way to preload routes in Angular 2 to prevent white flickering and jumping to top?

From Dev

Preload images using css

From Dev

How to preload images with PhoneGap?

From Dev

Preload multiple images with Glide

From Dev

WinRT preload images

From Dev

Preload multiple images with Glide

From Dev

swapping images on hover with preload

From Dev

Preload images android

From Dev

preload images in android

From Dev

The best way to toggle severals UIButtons with same images

From Dev

Best way to store and display images in Android

From Dev

Webview - Best way to upload images from device