angularjsHow can I use the AngularJS resize event to detect changes in window size?
The AngularJS resize event can be used to detect changes in window size. To use it, you need to inject the $window service into your controller and then listen for the resize event. Here is an example code block that shows how this is done:
app.controller('MyCtrl', function($scope, $window) {
$scope.windowSize = {
width: $window.innerWidth,
height: $window.innerHeight
};
angular.element($window).bind('resize', function() {
$scope.windowSize = {
width: $window.innerWidth,
height: $window.innerHeight
};
});
});
The code does the following:
- Injects the
$windowservice into the controller. - Sets the initial window size in the
$scope.windowSizevariable. - Binds the
resizeevent to the$windowobject and updates the$scope.windowSizevariable with the new window size when the event is triggered.
Helpful links
More of Angularjs
- How can I use AngularJS to transform XLTS files?
- How do I use AngularJS to watch for changes in a variable?
- How can I use AngularJS to construct an XSS payload?
- How do I use the window.open function with AngularJS?
- How do I create a link in AngularJS?
- How can I add a PDF viewer to my AngularJS application?
- How can I use AngularJS to watch for changes in my data?
- How do I use the AngularJS Wiki to find information about software development?
- How can I use AngularJS UI Router to create an application with multiple views?
- How do I use AngularJS to select an item from a list?
See more codes...