Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS event on window innerWidth size change

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?

like image 894
Liad Livnat Avatar asked Feb 07 '14 11:02

Liad Livnat


2 Answers

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

like image 124
Khanh TO Avatar answered Oct 06 '22 04:10

Khanh TO


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); });     

Unbind event

/**  * Window resize unbind event        */ angular.element($window).off('resize'); 
like image 28
lin Avatar answered Oct 06 '22 02:10

lin