Angularjs $watch on angular.element(document).height()报告的值不正确
我有一个根据文档高度执行计算的指令,每次更改文档高度时,我都必须重新进行计算 为了重新计算文档高度的变化,我在文档上设置了Angularjs $watch on angular.element(document).height()报告的值不正确,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有一个根据文档高度执行计算的指令,每次更改文档高度时,我都必须重新进行计算 为了重新计算文档高度的变化,我在文档上设置了$watch scope.$watch(function() { return angular.element(document).height(); }, function(newVal, oldVal) { scope.height = newVal; console.
$watch
scope.$watch(function() {
return angular.element(document).height();
}, function(newVal, oldVal) {
scope.height = newVal;
console.log('height: ', newVal);
});
最初,angular.element(document).height()报告的高度为664px
(更高)。展开(通过单击)其中一个div,高度报告为298px
(较低),现在通过展开/折叠播放,您将看到值始终更改为上一个值。
如果我打开控制台和scope.$apply()
,值会正确更新,但这不是解决方案,您不能在$watch
内运行scope.$apply()
,您将得到无限摘要循环
以下是问题演示:
怎样才能始终获得当前文档高度