angularjs如何找到img元素的高度和宽度

angularjs how to find img element height and width

本文关键字:高度 元素 何找 img angularjs      更新时间:2023-09-26

嗨,我正试图找到一个图像元素的高度和宽度来调整它的大小。然而,每次我使用elem.height()或elem.width()时,我都会得到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
  }
  });

请参阅下面的代码:

.directive('lightboxResizeImage', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
             console.log(elem.parent().width()); // =575
             console.log(elem[0].offsetHeight);
        }
    }
});