纯angularjs滚动事件监听器

Yon*_*xon 4 angularjs

我正在使用没有jQuery的angularjs,并尝试创建滚动事件侦听器。

尝试了这种方法:

  $rootScope.$watch(function() {
    return $window.scrollY;
  }, function(n,o) {
    console.log('scroll');
  });
Run Code Online (Sandbox Code Playgroud)

但这没用..

我设法通过使用此技术来创建调整大小的侦听器来实现此目标:

  $rootScope.$watch(function() { // Listens to window size change
    return $window.innerWidth;
  }, function(n, o) {
    console.log('resize');
  });
Run Code Online (Sandbox Code Playgroud)

有没有适当的方法来创建纯angularjs滚动侦听器?

Yon*_*xon 5

使用$ window创建滚动事件监听器'The Angular Way'实际上非常简单:

$window.onscroll = function() {
  console.log('scroll');
};
Run Code Online (Sandbox Code Playgroud)