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

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

但是,当这个代码被执行时,一个空行写入控制台。我试图显示元素的高度。有没有办法在AngularJS中不使用jQuery?

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?

谢谢!

推荐答案

看起来这是正常工作,并给出相同的结果,如果你使用:

It appears this is working correctly and gives the same result if you use:

element.style.height

由于在元素上未设置内联样式或CSS高度,因此将显示空白行。

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天全站免登陆