11

I know there are some similar question concerning this problem, however I none of the solutions could help me.

I am using AngularJS and want to detect the scroll event. I tried plenty of versions on how to get the event, however at most it fires when first loaded then never again.

My last code I tried was the following:

$($window).on('scroll', alert('scrolled'));

But I also tried this:

and many more, but nothing works.

Can anyone tell my what I am doing wrong?

Update: I tried the directive stuff, this works perfectly fine with safari BUT however not with chrome. Whats going on?

Update2: It works with the chrome mobile view when using the shift key. Are there any restrictions on chrome to apple trackpad? WTF?

Community
  • 1
  • 1
threxx
  • 1,213
  • 1
  • 31
  • 59
  • I think it could depend on where you are using this code. In a controller, in the directive link, etc. Can you post some more code? – Red2678 May 03 '16 at 21:42
  • The dom is probably not ready at the point you're adding the event listener. Add it in a directive's link method and it should work. Please have a look at the answer to this [SO question](http://stackoverflow.com/questions/20253322/angular-js-scroll-window). – AWolf May 03 '16 at 21:42

2 Answers2

8

I would just use

$(window).scroll(function () { alert('scrolled') })

OR you could use

angular.element($window).bind("scroll", function(e) {
    alert('scrolled')
})
James Freund
  • 473
  • 4
  • 13
3

there is no need for angular.element() inject $window to your controller, use the window.onscroll event

$window.onscroll = function (){ console.log('window was scrolled!'); };

Isaac Weingarten
  • 977
  • 10
  • 15