Skip to content

Instantly share code, notes, and snippets.

@alpercitak
Created March 25, 2015 09:51
Show Gist options
  • Save alpercitak/0862b3926b370197a019 to your computer and use it in GitHub Desktop.
Save alpercitak/0862b3926b370197a019 to your computer and use it in GitHub Desktop.
angularjs lazy image loading directive
var module = angular.module('ng-lazy', []);
module.directive("ngLazy", function() {
return {
link : function(scope, element, attrs) {
var img, loadImage;
img = null;
loadImage = function() {
element[0].src = attrs.ngLazyPlaceholder;
img = new Image();
img.src = attrs.ngLazy;
img.onload = function() {
element[0].src = attrs.ngLazy;
if (typeof (scope.ngLazyCallback) !== 'undefined') {
scope.ngLazyCallback();
}
};
};
scope.$watch((function() {
return attrs.ngLazy;
}), function(newVal, oldVal) {
loadImage();
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment