Scroll to anchor after page load in Angular Scroll to anchor after page load in Angular angularjs angularjs

Scroll to anchor after page load in Angular


So as pointed out, the $anchorScroll has to occur after the page has been rendered, otherwise the anchor doesn't exist. This can be achieved using $timeout().

$timeout(function() {  $anchorScroll('myAnchor');});

You can see this plunkr. Make sure to view it in pop-out mode (the little blue button in the upper right corner of the output screen).


Holy moly, this can be accomplished by simply adding autoscroll="true" to your template:

<div autoscroll="true" data-ng-include='"/templates/partials/layout/text-column.html"'></div>

Documentation


In my case using fragment in Router did not work.ViewportScroller did not work either. Even if they did - I'd prefer to keep URL clean.So, this is what I used:

(document.querySelector('.router-wrapper') as HTMLElement)?.scrollIntoView();