我想打算用 offsetHeight 获取图片的高度 。
angular.element(img).ready 这个是dom树加载完就立即执行了。此时图片还没显示出来;所以offsetHeight 获取的高度是0.
想用onload 但是在controller 里面好像是无效的。
要引入angular版的jquery 叫jqLite
angular.element('#img').height();
或者
angular.element(document.querySelector('#img'))[0].offsetHeight
<body ng-app="myModule">
<h1 my-directive>Hello Plunker! width: {{width}}, height: {{height}}
</h1>
</body>
angular.module('myModule', [])
.directive('myDirective', function($timeout) {
return {
restrict: 'A',
link: function(scope, element) {
scope.height = element.prop('offsetHeight');
scope.width = element.prop('offsetWidth');
}
};
})
;