I'm looking for a way to watch changes on window inner width size change. I tried the following and it didn't work:
$scope.$watch('window.innerWidth', function() { console.log(window.innerWidth); });
any suggestions?
We could do it with jQuery:
$(window).resize(function(){ alert(window.innerWidth); $scope.$apply(function(){ //do something to update current scope based on the new innerWidth and let angular update the view. }); });
Be aware that when you bind an event handler inside scopes that could be recreated (like ng-repeat scopes, directive scopes,..), you should unbind your event handler when the scope is destroyed. If you don't do this, everytime when the scope is recreated (the controller is rerun), there will be 1 more handler added causing unexpected behavior and leaking.
In this case, you may need to identify your attached handler:
$(window).on("resize.doResize", function (){ alert(window.innerWidth); $scope.$apply(function(){ //do something to update current scope based on the new innerWidth and let angular update the view. }); }); $scope.$on("$destroy",function (){ $(window).off("resize.doResize"); //remove the handler added earlier });
In this example, I'm using event namespace from jQuery. You could do it differently according to your requirements.
Improvement: If your event handler takes a bit long time to process, to avoid the problem that the user may keep resizing the window, causing the event handlers to be run many times, we could consider throttling the function. If you use underscore, you can try:
$(window).on("resize.doResize", _.throttle(function (){ alert(window.innerWidth); $scope.$apply(function(){ //do something to update current scope based on the new innerWidth and let angular update the view. }); },100));
or debouncing the function:
$(window).on("resize.doResize", _.debounce(function (){ alert(window.innerWidth); $scope.$apply(function(){ //do something to update current scope based on the new innerWidth and let angular update the view. }); },100));
Difference Between throttling and debouncing a function
No need for jQuery! This simple snippet works fine for me. It uses angular.element() to bind window resize event.
/** * Window resize event handling */ angular.element($window).on('resize', function () { console.log($window.innerWidth); });
/** * Window resize unbind event */ angular.element($window).off('resize');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With