在 AngularJS 中获取没有 JQuery 的 HTML 元素高度
我开始熟悉 AngularJS.我尽量做到纯洁".出于这个原因,我试图避免包含 jQuery.但是,我在获取 HTML 元素的高度时遇到了挑战.目前,我正在尝试以下操作:
I'm getting familiar with AngularJS. I'm trying to be as 'pure' as I can. For that reason, I'm trying to avoid including jQuery. However, I'm having a challenge getting an HTML element's height. Currently, I'm trying the following:
angular.module('myModule', [])
.directive('myDirective', function() {
return {
restrict: 'A',
link: function(scope, element) {
console.log(element.css('height'));
}
};
})
;
然而,当这段代码被执行时,一个空行被写入控制台.我正在尝试显示元素的高度.有没有办法在不使用 jQuery 的情况下在 AngularJS 中做到这一点?
However, when this code gets executed, an empty line gets written to the console. I'm trying to display the height of the element. Is there a way to do this in AngularJS without using jQuery?
谢谢!
如果你使用:
element.style.height
由于没有在元素上设置内联样式或 CSS 高度,因此显示空行.您可以直接获取 HTML 元素的高度,而不是依赖样式.
Since no inline style or CSS height is set on the element a blank line is shown. Instead of relying on the style you can get the HTML elements height directly.
console.log(element[0].offsetHeight);