1

I'm using AngularJS to display an image. The image's height and width is dynamic depending on the browser's height and width. How can I only display a div that contains the image only when the image is ready to be displayed?

<div id="container">
  <img ng-src="{{media_list.cur}}" />
</div>

Do I need to use jQuery for this? I'd like to do as much as possible without any additional libraries (maybe I can use AngularJS's built-in jqlite or something)

2 Answers 2

2

Something like this might work.

app.directive('img', function() {
  return {
    restrict: 'E',
    link: function(scope, el, attrs) {
      el.addClass('hide');

      el.on('load', function() {
        el.removeClass('hide');
      });
    }
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Do you have an example of what the CSS & HTML components might look like?
<img src="path/to/img.jpg"> & img.hide { display: none; } or whichever method of hiding the image you prefer, opacity, visibility etc
1

Believe you can use the onload property if you want to avoid jQuery:

img.onload = function() {
  // image is loaded
}

3 Comments

You have to get the img elements from the DOM too, but this would work.
Thanks. Do you have an example of what the HTML/CSS would look like as well?
@jyek el.on if jquery is not avaible is not jquery but jqLite see docs.angularjs.org/api/ng/function/angular.element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.