【问题标题】:Window.scroll not working in augular controllerWindow.scroll 在角度控制器中不起作用
【发布时间】:2015-11-09 00:12:58
【问题描述】:

我尝试在用户滚动到底部时提醒消息。我正在使用 angularJS,但它似乎没有工作。

app.controller('MainController',function($scope, $rootScope, $route, $http, $timeout){

  // overflow auto 
  $(document).ready(function() {
      $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() == $(document).height()) {
           alert("bottom!");
        }
      });
  });
});

有什么帮助吗?

【问题讨论】:

  • 仅供参考,不建议在控制器中绑定事件。你应该为此制定一个指令。无论如何,您有控制台错误吗?
  • 另外,您可以将$window 对象注入您的控制器或指令中。看看有没有帮助。

标签: jquery angularjs window scrolltop


【解决方案1】:

你快到了。正如 Dvir 指出的那样,如果您将所有 DOM 交互保留在指令中,那么测试您的控制器会更容易。所以,你可以这样做:

angular.module("myApp", [])
    .directive('myDirective', function() {
        return {
            link: function(scope, element, attrs){
                $(window).scroll(function() {   
                   if($(window).scrollTop() + $(window).height() == $(document).height()) {
                       alert("bottom!");
                   }
                });
            }
        };
    });

这是一个有效的小提琴:https://jsfiddle.net/rdvjjav2/1/

【讨论】:

  • 如果你不想使用 'ng-infinite-scroll' 模块并完成简单的无限滚动,我认为这是一个不错的方法。
猜你喜欢
  • 2015-06-02
  • 2018-05-19
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 2017-10-25
  • 2014-12-14
  • 1970-01-01
  • 2017-06-03
相关资源
最近更新 更多