我试图在 ng-repeat 中获取当前元素的高度。
当我记录 element
时,我可以在控制台中看到具有 clientHeight
属性的整个对象。但是如果我单独记录 element[0].clientHeight
控制台返回 undefined
..
我做错了什么?
JS
app.directive('photo', function(){
return {
restrict: 'A',
link: function(scope, element, attrs) {
console.log( element[0].clientHeight);
}
}
});
HTML
<div ng-repeat="media in mediaScope" class="gallery_media col-xs-6">
<img photo ng-src="{{ media.url }}">
</div>
最佳答案
您需要在该元素上设置 onload
事件,当 img 从 src
属性加载时将被调用
指令
app.directive('photo', function(){
return {
restrict: 'E',
link: function(scope, element, attrs) {
element.on('load', function(){
console.log( element[0].clientHeight);
});
}
}
});
关于javascript - 在 ng-repeat 中获取元素的比例在 Angularjs 中返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003813/