Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何像在JQuery中一样在AngularJS中获得高度?_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript 如何像在JQuery中一样在AngularJS中获得高度?

Javascript 如何像在JQuery中一样在AngularJS中获得高度?,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,下面是我的jquery代码片段: $(document).ready(function() { var height = Math.max($("#one").height(), $("#two").height()); $("#one").height(height); $("#two").height(height); }); 我想把它转换成AngularJS,但我一直在讨论如何获得高度。我尝试了很多不同的方法,包括offsetHeight和scrollHeig

下面是我的jquery代码片段:

  $(document).ready(function() {
    var height = Math.max($("#one").height(), $("#two").height());
    $("#one").height(height);
    $("#two").height(height);
});
我想把它转换成AngularJS,但我一直在讨论如何获得高度。我尝试了很多不同的方法,包括offsetHeight和scrollHeight,它们都返回0。是因为我在用桌子吗?我不知道该怎么做/我是否做对了。以下是我到目前为止一直在尝试做的事情的概要:

 $scope.height = function()
{
    var height = window.Math.max(HEIGHT OF MY ELEMENT "firstTable", HEIGHT OF MY ELEMENT "secondTable");
    //Now get the height and set it.
};
我不确定是否应该将其写入指令并将其放在我的表中,以便访问$element或其他什么


提前感谢

我建议使用普通的老javascript来实现这一点

看起来这很有效:

document.getElementById('someDiv').clientHeight;
// clientHeight includes the height and vertical padding.
document.getElementById('someDiv').offsetHeight;
// offsetHeight includes the height, vertical padding, and vertical borders.
document.getElementById('someDiv').scrollHeight;
// scrollHeight includes the height of the contained document (would be greater than just height in case of scrolling), vertical padding, and vertical borders.

找到了Stackoverflow的解决方案:所以我没有任何优点

包含在Angular中的jqLite是有限的,不提供计算高度的函数

您最好的选择是将元素注入控制器, 并通过以下方式获取高度:

element[0].offsetHeight;

plnkr上的演示:

您想要的不是使用AngularJS来获取高度,而是使用本机JavaScript

使用document.getElementById选择元素,然后使用.offsetHeight获取高度,最后使用.style.height设置高度

您的代码看起来有点像这样:

var elementOne = document.getElementById("one"),
    elementTwo = document.getElementById("two"),
    height = Math.max(elementOne.offsetHeight, elementTwo.offsetHeight);
elementOne.style.height = height;
elementTwo.style.height = height;
注意,我为每个元素创建了变量,以避免通过document.getElementById重复检索元素以获取和设置高度