How to solve "Cannot read property 'scrollIntoView' of undefined"?

24,586

Solution 1

Try angular ViewportScroller Service Which provide scrollToAnchor method

scroll.html

<button (click)="scroll('target')">Click to scroll</button>
<div id="target">Your target</div>

scroll.ts

import { Component, Input } from '@angular/core';
import { ViewportScroller } from '@angular/common';
@Component({
  selector: 'scroll',
  template: `
    <button (click)="scroll('target')">Click to scroll</button>
    <div id="target">Your target</div>
  `,
  styles: [`h1 { font-family: Lato; }`, `div { margin-top: 5000px; }`]
})
export class ScrollComponent {

  constructor(private vps: ViewportScroller) {

  }
  scroll(id) {
    this.vps.scrollToAnchor(id);
  }
}

Example:https://stackblitz.com/edit/angular-scroll-local-variable-99hwvo

Solution 2

Try using ViewChild:

//HTML 
<button (click)="scroll()"></button><div #target>Your target</div>


//ts

//Import
import { ..., ViewChild, ElementRef } from '@angular/core';

//Declare
@ViewChild('target') targetEl: ElementRef;

scroll() {
    this.targetEl.nativeElement.scrollIntoView();
}
Share:
24,586
mani singh
Author by

mani singh

Updated on August 01, 2022

Comments

  • mani singh
    mani singh almost 2 years

    I want to scroll to a particular div on clicking a button in Angular 7, below is the code I am using but it is working in stackblitz but showing error when i use in my project. "Cannot read property 'scrollIntoView' of undefined".

    https://stackblitz.com/edit/angular-scroll-local-variable?file=src%2Fapp%2Fscroll.component.ts

    try this link: https://stackblitz.com/edit/angular-scroll-local-variable-ja96uz?file=src%2Fapp%2Fapp.component.html

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

    and in component:

    scroll(el) {
        el.scrollIntoView();
    }
    
  • mani singh
    mani singh over 5 years
    i have tried and now it is showing "Cannot read property 'scrollIntoView' of null"
  • mani singh
    mani singh over 5 years
    I tried and it is showing "Cannot read property 'nativeElement' of undefined"
  • mani singh
    mani singh over 5 years
    @PrashantPimpale yes it is working in stackblitz but showing error mentioned above in question when i use with html in different file and ts code in different.
  • mani singh
    mani singh over 5 years
    @Chellappan thanks error is not showing now but it is not getting scroll to the div i need. i have this in my html > line a: <mat-radio-button value="1" (click)="onButtonClick(); scroll('target');">Cash</mat-radio-button> AND AT line b: <div id="target"> <ion-card *ngIf="buttonClicked"> //some data in card </ion-card> </div>
  • mani singh
    mani singh over 5 years
    @PrashantPimpale i have this in my html > line a: <mat-radio-button value="1" (click)="onButtonClick(); scroll('target');">Cash</mat-radio-button> AND AT line b: <div id="target"> <ion-card *ngIf="buttonClicked"> //some data in card </ion-card> </div>
  • mani singh
    mani singh over 5 years
    i tried this but it is not working can u please share stackblitz working of it?
  • mani singh
    mani singh over 5 years
    can u please check ur link ?? it is not working as expected
  • mani singh
    mani singh over 5 years
    @Chellappan your solution is not making page scrolled to the id given div. you can check here what i am trying to achieve. stackblitz.com/edit/…
  • Chellappan வ
    Chellappan வ over 5 years
    you are not using Viewportscroller
  • mani singh
    mani singh over 5 years
    @Chellappan I had but with that too didn't provided the result. No scrolling was there.
  • Velusamy Venkatraman
    Velusamy Venkatraman over 4 years
    this answer only for exception handler. but need a solution for that.
  • Prashant Pimpale
    Prashant Pimpale over 4 years
    You must have checked the stackblitz, did you?