I have this as a way to watch for a window resize event. However it works very slowly. Can someone advise if they know of some other way that is simple and does not involve a complicated directive. Also would appreciate any suggestions on if the way I am doing it is a good way.
$scope.$watch(($scope) => {
$scope.isLarge = $window.innerWidth > 650 ? true : false;
angular.element($window).on('resize', () => {
$scope.$digest();
});
console.log($scope.isLarge);
});
Working Fiddle: 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();
});
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.