gpt4 book ai didi

javascript - 当用户更改窗口高度时如何触发 Angular 中的事件?

转载 作者:行者123 更新时间:2023-12-03 06:00:43 25 4
gpt4 key购买 nike

每次用户更改窗口高度时,我都需要计算元素的位置。因此我创建了一个指令。

  'use strict';
angular.module "myApp"
.directive 'uibPosition',['$position','$document','$timeout', ($position,$document,$timeout)->
return {
restrict: 'EAC',
link: (scope, element, attr) ->
//I want to Trigger this event when user change windows height
$timeout ->
if $document.height() - $position.position(element).top < 500
element[0].querySelector('.custom-popup-wrapper').style.top = 'auto'
element[0].querySelector('.custom-popup-wrapper').style.bottom = 40 + 'px'
else
element[0].querySelector('.custom-popup-wrapper').style.top = $position.position(element).top+40+'px'
}
]

怎么做?我真的很感激

最佳答案

您可以将观察者添加到指令中:

        var w = angular.element($window);

scope.getWindowDimensions = function () {
return {
"h": w.height(),
"w": w.width()
};
};
var watcherTimeout;
scope.$watch(scope.getWindowDimensions, function (newValue) {
$timeout.cancel(watcherTimeout);
watcherTimeout = $timeout(function(){
scope.callback(); // call callback function
}, 300);
}, true);

w.bind("resize", function () {
scope.$apply();
});

关于javascript - 当用户更改窗口高度时如何触发 Angular 中的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com