Scroll to element on click in Angular 4

You could do it like this:

<button (click)="scroll(target)">Scroll To Div</button>
<div #target>Your target</div>

and then in your component:

scroll(el: HTMLElement) {
    el.scrollIntoView();
}

Edit: I see comments stating that this no longer works due to the element being undefined. I created a StackBlitz example in Angular 7 and it still works. Can someone please provide an example where it does not work?

Leave a Comment