0

Using Angular How can I watch an image to see if it has finished loading inside a directive?

For example show an alert when the image has been fully retrieved from the URL (not when the div is rendered).

html:

 <img src="{{thumbnail}}" image-loading-spinner/>

directive:

directive('imageLoadingSpinner', ['$document', '$parse', function ($document, $parse) {

    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
             // get item to watch?
             scope.$watch(????, function (newValue) {
               //has loaded image url?
               alert('got the image!')
          )}

        }
    };


}]). 
Prometheus
  • 32,405
  • 54
  • 166
  • 302

1 Answers1

1

There would not be an existing watch expression for that. You could code your a custom watch function, but I would advise against that. Watches are evaluated quite often. And what you're looking for is single event.

You should look into reacting to both load and error events instead.

Kevin Sandow
  • 4,003
  • 1
  • 20
  • 33