Window AngularJS 中的调整大小事件

Window resize event in AngularJS

我已经谷歌搜索了一段时间,到目前为止我发现了如何使用 window 调整大小指令调整 window 的大小,但在我的代码中我使用 window.innerHeight 来获得初始高度.我可以随时使用它,除非我需要一个事件来改变它。 JavaScript window resize event 出于某种原因没有为我开火。这是我在控制器内部的代码:

function adjustH() {
    var bodyElem = document.getElementsByTagName('body')[0];
    var topP = window.getComputedStyle(bodyElem, null).getPropertyValue('padding-top');
    topP = topP.substring(0, topP.indexOf("px"));
    var bottomP = window.getComputedStyle(bodyElem, null).getPropertyValue('padding-bottom');
    bottomP = bottomP.substring(0, bottomP.indexOf("px"));
    vm.h = window.innerHeight - topP - bottomP - 20;
}

window.onresize = function(event) {
    adjustH();
}

谁能告诉我如果这样做是不可能的,我必须走指令路线吗?或者,如果可能的话,请告诉我我缺少什么。

如果您正在使用 angularJS,您是否将 $window 作为依赖项传递给您的控制器。

我之前遇到过类似的问题,那是因为没有设置depoendency。 希望这有帮助。

阅读您的评论后

Also my canvas set to height="{{vm.h}}" but it only resizes when I mouse over

看来您需要调用$scope.$apply()(或$rootScope.$apply())才能更新元素的高度属性。

如果您从 directive/controller 添加更多代码,我可以为您提供更详细的答案。