Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs how to find img element height and width

Hi I'm trying to find an image element's height and width in order to resize it. However everytime I use elem.height() or elem.width() I get 0?

html

<div>
    <img data-ng-src="{{photosrc}}" lightbox-resize-image>
</div>

directive.js

.directive('lightboxResizeImage', function() {
return {
    restrict: 'A',
    link: function(scope, elem, attrs) {

        console.log(elem.parent().width()); //==equals 575
        console.log(elem.width()); //this always equals 0

  }
  });
like image 350
user1424508 Avatar asked Nov 01 '22 09:11

user1424508


1 Answers

See code below:

.directive('lightboxResizeImage', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
             console.log(elem.parent().width()); // =575
             console.log(elem[0].offsetHeight);
        }
    }
});
like image 161
trinhvanhuy Avatar answered Nov 11 '22 14:11

trinhvanhuy