在 AngularJS 中获取没有 JQuery 的 HTML 元素高度 [英] Get HTML Element Height without JQuery in AngularJS

查看:39
本文介绍了在 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);

http://plnkr.co/edit/03YWwjBjYpid4fVmDxlM?p=preview

如何检索 HTML 元素的实际宽度和高度?

这篇关于在 AngularJS 中获取没有 JQuery 的 HTML 元素高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆