窗口内部宽度更改的AngularJS事件

时间:2014-02-07 11:17:08

标签: javascript jquery angularjs angularjs-scope

我正在寻找一种方法来观察窗口内部宽度变化的变化。我尝试了以下内容并且它没有工作:

$scope.$watch('window.innerWidth', function() {
     console.log(window.innerWidth);
});

有什么建议吗?

4 个答案:

答案 0 :(得分:142)

我们可以用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.
    });
});

请注意,当您在可以重新创建的范围内绑定事件处理程序时(如ng-repeat范围,指令范围,...),您应该在范围被销毁时取消绑定事件处理程序。如果不这样做,每次重新创建范围(重新运行控制器)时,将再添加1个处理程序,从而导致意外行为和泄漏。

在这种情况下,您可能需要识别附加的处理程序:

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

在这个例子中,我正在使用jQuery中的event namespace。你可以根据自己的要求做不同的事情。

改进:如果你的事件处理程序需要花费很长时间来处理,为了避免用户可能继续调整窗口大小的问题,导致事件处理程序多次运行,我们可以考虑限制该功能。如果您使用underscore,则可以尝试:

$(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));

debouncing 该功能:

$(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

答案 1 :(得分:40)

不需要jQuery!这个简单的代码段对我来说很好。它使用angular.element()来绑定窗口大小调整事件。

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

解除绑定事件

/**
 * Window resize unbind event      
 */
angular.element($window).off('resize');

答案 2 :(得分:23)

我找到了一个可能有帮助的小提琴:http://jsfiddle.net/jaredwilli/SfJ8c/

我重构了代码,使其更简单。

// In your controller
var w = angular.element($window);
$scope.$watch(
  function () {
    return $window.innerWidth;
  },
  function (value) {
    $scope.windowWidth = value;
  },
  true
);

w.bind('resize', function(){
  $scope.$apply();
});

然后您可以从html

引用windowWidth
<span ng-bind="windowWidth"></span>

答案 3 :(得分:9)

如果Khanh TO的解决方案为您造成了UI问题(就像它对我而言)尝试使用$timeout来更新属性,直到它保持500毫秒不变。

var oldWidth = window.innerWidth;
$(window).on('resize.doResize', function () {
    var newWidth = window.innerWidth,
        updateStuffTimer;

    if (newWidth !== oldWidth) {
        $timeout.cancel(updateStuffTimer);
    }

    updateStuffTimer = $timeout(function() {
         updateStuff(newWidth); // Update the attribute based on window.innerWidth
    }, 500);
});

$scope.$on('$destroy',function (){
    $(window).off('resize.doResize'); // remove the handler added earlier
});

参考:https://gist.github.com/tommaitland/7579618

相关问题