0

I have a fixed top menu in Angular but when I try to switch page (within the app) the browser windows is not scrolling up.

I try to do this in my controller (this code is loaded every time i switch page within the app):

$('body').scroll();
window.scrollTo(0, 0);
$("html, body").animate({ scrollTop: 0 }, 100);

None of which is working.

Is there any trick that i am missing?

I tried this as well:

$location.hash('#top');
// call $anchorScroll()
$anchorScroll();

but without success

1 Answer 1

3

First of all, you should never attach DOM events or modify DOM from controller. You can use directives for that.

As per you question - AngularJS has an event called '$routeChangeSuccess'. You can attach handler to this event with $scope.$on('$routeChangeSuccess', function () {...}) and run scrolling code inside.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey Thanks Patryk, I know but i am just trying everything out there to see what is working, I try your code and the listener is triggered but still it's not going up, with jQuery / window / or anchorScroll
What version of angular are you using?
k your fix worked, i had another JavaScript code that's was causing the problem (of your fix not working) ;) Thank you!

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.