I am having trouble getting a smooth scroll service to work in angular 2. Are there any services for smooth scrolling, or plain anchor scrolling, that might work until the
For anyone still on the search for a smooth scroll @alex-j 's answer works great for me in Angular 2.0 - but I had to change the Window service to this :-
import { Injectable } from '@angular/core';
function _window() : any {
// return the global native browser window object
return window;
}
@Injectable()
export class WindowRef {
get nativeWindow() : any {
return _window();
}
}
All props to this blog http://juristr.com/blog/2016/09/ng2-get-window-ref/ - now I have a smooth scroll service I can call from anywhere :)
If you want a very simple anchor jump that works after routing and within routed views, you can also use ng2-simple-page-scroll.
<a simplePageScroll href="#myanchor">Go there</a>
Or right after routing:
<a simplePageScroll [routerLink]="['Home']" href="#myanchor">Go there</a>
It does a simple instant jump, but it works.
example:
function goToElement(elemId){
let element = window.getElementById(elemId);
element.scrollIntoView({behavior: "smooth"});
}