[英]How can I react to changes in window size with AngularJS?
我用這種方法來監視窗口調整大小事件。 但是,它的工作非常緩慢。 有人可以建議他們是否知道其他簡單且不涉及復雜指令的方式。 如果我正在做的這是一個好方法,也將不勝感激。
$scope.$watch(($scope) => {
$scope.isLarge = $window.innerWidth > 650 ? true : false;
angular.element($window).on('resize', () => {
$scope.$digest();
});
console.log($scope.isLarge);
});
工作小提琴: https : //jsfiddle.net/jaredwilli/SfJ8c/
的HTML
<div ng-app="miniapp" ng-controller="AppController" ng-style="style()" resize>window.height: {{windowHeight}}
<br />window.width: {{windowWidth}}
<br />
</div>
的JavaScript
var app = angular.module('miniapp', []);
function AppController($scope) {
/* Logic goes here */
}
app.directive('resize', function ($window) {
return function (scope, element) {
var w = angular.element($window);
scope.getWindowDimensions = function () {
return {
'h': w.height(),
'w': w.width()
};
};
scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
scope.windowHeight = newValue.h;
scope.windowWidth = newValue.w;
scope.style = function () {
return {
'height': (newValue.h - 100) + 'px',
'width': (newValue.w - 100) + 'px'
};
};
}, true);
w.bind('resize', function () {
scope.$apply();
});
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.