我试图在 angularjs 中获取一个 div 的高度,但它只返回初始值并且从不更新:
vm.summaryHeight = $(".history-log-container").height();
console.log(vm.summaryHeight);//returns 0 and then never logs again.
如何在更新时获取此 div 的高度?
我试过这个:
link: function ($scope, element, attrs) {
//console.log(element.height());
$scope.$watch(function(){
return element.height();
}, function(oldVal, newVal){
//returns the initial values then nothing else
console.log(arguments);
})
}
最佳答案
正如其他人所说,除非您另有说明,否则该值将被取一次。尝试类似的东西;
$(document).resize(function(){
// Your code here
});
如果您的窗口大小更改会影响目标元素的高度,那么无论何时调整文档大小时,该值都将被重新获取。如果您要定位元素而不是文档,这也可能有效;
$('.history-log-container').resize(function(){
// Your code here
});
但您必须检查一下 - 我通常只在一个文档大小调整函数中设置所有窗口响应大小。
关于css - 获取div angular js的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162605/