How to call scrollIntoView on an element in angular 2+

angular

Solution

First add a template reference variable in the element (the `#myElem`):

<p #myElem>Scroll to here!</p>

Then create a property in the component with attribute `ViewChild`, and call `.nativeElement.scrollIntoView` on it:

export class MyComponent {
  @ViewChild("myElem") MyProp: ElementRef;

  ngOnInit() {
    this.MyProp.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
  }
}

Problem

I want to call scrollIntoView on a HTML element in a component. In angular.js 1 I can do something like this in the controller: ``` var element = $window.document.getElementById(id); element.scrollIntoView({ behavior: "smooth", block: "start" }); ``` How can I do the same thing in angular 2+ ?

Original source