AngularJS event on window size change

JavaScript

I'm looking for a way to watch changes on window inner width size change in AngularJS. I've tried this approach but it doesn't work for me:

$scope.$watch('window.innerWidth', function() {
     console.log(window.innerWidth);
});
Software Question created: 2020-05-05 05:04 SpelunkiRU

2

You can achieve this by using the default events of angular.element(). You don't need jQuery for this. This small snippet will work for you:

/**
 * Window resize event watch
 */
angular.element($window).on('resize', function () {
    console.log($window.innerWidth);
}); 		

Don't forget to unbind the event:

/**
 * Window resize unbind event      
 */
angular.element($window).off('resize');
answered 2020-05-05 05:09 KhanTo