如何在用户更改 windows 高度时触发 Angular 中的事件?
how to Trigger the event in Angular when user change the windows height ?
我需要在每次用户更改时计算元素的位置 windows height.so 我创建了一个指令。
'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();
});
我需要在每次用户更改时计算元素的位置 windows height.so 我创建了一个指令。
'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();
});