Window.scroll 在角度控制器中不工作

Window.scroll not working in augular controller

我试图在用户滚动到底部时提醒一条消息。我正在使用 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!");
        }
      });
  });
});

有帮助吗?

你到了那里。正如 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!");
                   }
                });
            }
        };
    });

这是一个有效的 fiddle:https://jsfiddle.net/rdvjjav2/1/