Skip to content

Instantly share code, notes, and snippets.

@callmehiphop
Created August 13, 2014 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callmehiphop/3799372231f41e6bdf7b to your computer and use it in GitHub Desktop.
Save callmehiphop/3799372231f41e6bdf7b to your computer and use it in GitHub Desktop.
angular image preloader
angular
.module('preload', [])
.factory('preload', function ($q) {
'use strict';
function getImage (src, ignoreFailure) {
var defer = $q.defer();
var img = new Image();
var resolve = defer.resolve.bind(defer, img);
img.onload = resolve;
img.onerror = ignoreFailure ? resolve : defer.reject.bind(defer);
img.src = src;
return defer.promise;
}
function getImages (queue) {
queue = angular.isArray(queue) ? queue : [queue];
var requests = queue.map(function (src) {
return getImage(src, true);
});
return $q.all(requests);
}
return {
image: getImage,
images: getImages
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment