AngularJS event on window innerWidth size change

前端 未结 4 1408
再見小時候
再見小時候 2020-11-30 20:27

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\', fu         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 21:06

    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

提交回复
热议问题