12

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?

2
  • 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? Commented May 3, 2016 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. Commented May 3, 2016 at 21:42

2 Answers 2

8

I would just use

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

OR you could use

angular.element($window).bind("scroll", function(e) {
    alert('scrolled')
})
Sign up to request clarification or add additional context in comments.

3 Comments

Angular has some built in jquery functionality using angular.element
okay that works with Safari, however NOT with chrome. Any idea why this is so & how so fix it?
Try using a directive as shown in the jsfiddle. This will ensure your listener isn't created too late or too early.
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!'); };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.