Trying to scroll to an anchor link using the following syntax.
<a [routerLink]="['./']" fragment="test">Testing</a>
And the anchor node looks like this
<div id="test">
When clicked the browser address bar shows the #test fragment but the automatic scrolling does not occur. Any idea why it does not scroll?
I can suggest to user ng2-page-scroll
ng2-page-scroll
install
import in your app.module.ts
test it in your html component
Look in Angular 6 new feature
ViewportScroller
I liked using this
then in HTML do something like
I suppose scrolling isn't implemented with angular 2 yet. My solution to similar problem (scrolling to anchor on the same page) was to use ng2-page-scroll.
Based on @vsavkin workaround and taking advantage that fragments are provided as an observable (using
"@angular/core": "^2.3.1"
):From Angular 6.1 there is
anchorScrolling
for the router:Set this in app.module.ts
html
Import now the viewScroller which Angular v6 new feature (this might be not neccesary):