Created
March 25, 2015 09:51
-
-
Save alpercitak/0862b3926b370197a019 to your computer and use it in GitHub Desktop.
angularjs lazy image loading directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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