Angular 2 scroll to top on route change not working












18














I am trying to scroll to the top of my page on my angular 2 site when the route changes, I have tried the following, but nothing happens, when I change the route from one page to another, the page is scrolled to where it was on the first page:



import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
selector: 'my-app',
template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {
constructor(private router: Router) { }

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}


What am I doing wrong?










share|improve this question






















  • Try setTimeout(function () {window.scrollTo(0, 300);},2)
    – User3250
    Nov 7 '18 at 2:37










  • Nope, that did not work, I get no errors in my console log.
    – user979331
    Nov 7 '18 at 3:21










  • I tried that it worked for me. Other option is using jquery $('#content').animate({ scrollTop: 20 }, 200).
    – User3250
    Nov 7 '18 at 3:23












  • Still does not work, this is annoying
    – user979331
    Nov 7 '18 at 3:47






  • 1




    Can you provide a stackblitz showing the problem?
    – ConnorsFan
    Nov 13 '18 at 0:39
















18














I am trying to scroll to the top of my page on my angular 2 site when the route changes, I have tried the following, but nothing happens, when I change the route from one page to another, the page is scrolled to where it was on the first page:



import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
selector: 'my-app',
template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {
constructor(private router: Router) { }

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}


What am I doing wrong?










share|improve this question






















  • Try setTimeout(function () {window.scrollTo(0, 300);},2)
    – User3250
    Nov 7 '18 at 2:37










  • Nope, that did not work, I get no errors in my console log.
    – user979331
    Nov 7 '18 at 3:21










  • I tried that it worked for me. Other option is using jquery $('#content').animate({ scrollTop: 20 }, 200).
    – User3250
    Nov 7 '18 at 3:23












  • Still does not work, this is annoying
    – user979331
    Nov 7 '18 at 3:47






  • 1




    Can you provide a stackblitz showing the problem?
    – ConnorsFan
    Nov 13 '18 at 0:39














18












18








18


1





I am trying to scroll to the top of my page on my angular 2 site when the route changes, I have tried the following, but nothing happens, when I change the route from one page to another, the page is scrolled to where it was on the first page:



import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
selector: 'my-app',
template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {
constructor(private router: Router) { }

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}


What am I doing wrong?










share|improve this question













I am trying to scroll to the top of my page on my angular 2 site when the route changes, I have tried the following, but nothing happens, when I change the route from one page to another, the page is scrolled to where it was on the first page:



import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
selector: 'my-app',
template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {
constructor(private router: Router) { }

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}


What am I doing wrong?







javascript jquery angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 '18 at 1:37









user979331user979331

8629115219




8629115219












  • Try setTimeout(function () {window.scrollTo(0, 300);},2)
    – User3250
    Nov 7 '18 at 2:37










  • Nope, that did not work, I get no errors in my console log.
    – user979331
    Nov 7 '18 at 3:21










  • I tried that it worked for me. Other option is using jquery $('#content').animate({ scrollTop: 20 }, 200).
    – User3250
    Nov 7 '18 at 3:23












  • Still does not work, this is annoying
    – user979331
    Nov 7 '18 at 3:47






  • 1




    Can you provide a stackblitz showing the problem?
    – ConnorsFan
    Nov 13 '18 at 0:39


















  • Try setTimeout(function () {window.scrollTo(0, 300);},2)
    – User3250
    Nov 7 '18 at 2:37










  • Nope, that did not work, I get no errors in my console log.
    – user979331
    Nov 7 '18 at 3:21










  • I tried that it worked for me. Other option is using jquery $('#content').animate({ scrollTop: 20 }, 200).
    – User3250
    Nov 7 '18 at 3:23












  • Still does not work, this is annoying
    – user979331
    Nov 7 '18 at 3:47






  • 1




    Can you provide a stackblitz showing the problem?
    – ConnorsFan
    Nov 13 '18 at 0:39
















Try setTimeout(function () {window.scrollTo(0, 300);},2)
– User3250
Nov 7 '18 at 2:37




Try setTimeout(function () {window.scrollTo(0, 300);},2)
– User3250
Nov 7 '18 at 2:37












Nope, that did not work, I get no errors in my console log.
– user979331
Nov 7 '18 at 3:21




Nope, that did not work, I get no errors in my console log.
– user979331
Nov 7 '18 at 3:21












I tried that it worked for me. Other option is using jquery $('#content').animate({ scrollTop: 20 }, 200).
– User3250
Nov 7 '18 at 3:23






I tried that it worked for me. Other option is using jquery $('#content').animate({ scrollTop: 20 }, 200).
– User3250
Nov 7 '18 at 3:23














Still does not work, this is annoying
– user979331
Nov 7 '18 at 3:47




Still does not work, this is annoying
– user979331
Nov 7 '18 at 3:47




1




1




Can you provide a stackblitz showing the problem?
– ConnorsFan
Nov 13 '18 at 0:39




Can you provide a stackblitz showing the problem?
– ConnorsFan
Nov 13 '18 at 0:39












5 Answers
5






active

oldest

votes


















13





+100









The router will emit an event when a new component gets loaded in the <router-outlet> so you can attach an event to it.



So in your component with <router-outlet> use:



<router-outlet (activate)="scrollTop($event)">



and then in the same component where you placed <router-outlet> add the following method:



scrollTop(event) {
window.scroll(0,0);
}





share|improve this answer





















  • I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
    – user979331
    Nov 14 '18 at 16:42










  • Actually, this does work, but only when I remove height: 100% to the body
    – user979331
    Nov 14 '18 at 16:46










  • This answer appears to be correct, gonna do some testing on it first.
    – user979331
    Nov 14 '18 at 16:46



















2














Wait for the component to initialized component before you start scrolling. So better to put this code under ngAfterViewInit function.



 ngAfterViewInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}





share|improve this answer





















  • Trying this now.
    – user979331
    Nov 7 '18 at 19:26










  • Nope this does not work
    – user979331
    Nov 7 '18 at 19:28










  • Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
    – Sunil Singh
    Nov 7 '18 at 19:29










  • I put a console.log right before window.scrollTo(0, 0) and it is not being executed
    – user979331
    Nov 7 '18 at 20:45










  • Wait my bad, the console log is getting executed.
    – user979331
    Nov 7 '18 at 20:47



















2














This similar issue was faced by me which was due to the style applied to body.
i.e.



body {
height: 100%;
overflow-x: hidden;
}


If I removed this style then my layout was badly affected.



Instead of removing style I tried below solution and it worked for me...



Solution:



export class AppComponent implements OnInit {


constructor(private router: Router, private changeDetect: ChangeDetectorRef) {
}

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
// Change height:100% into auto
$('body').css('height', 'auto');
// Successfully scroll back to top
$('body').scrollTop(0);
// Remove javascript added styles
$('body').css('height', '');
this.changeDetect.detectChanges();
});

}
}





share|improve this answer































    2














      constructor(
    private router: Router,
    private ngZone: NgZone) {
    router.events.subscribe((event: RouterEvent) => {
    this._navigationInterceptor(event);
    });
    }

    private _navigationInterceptor(event: RouterEvent): void {
    if (event instanceof NavigationStart) {
    }
    if (event instanceof NavigationEnd) {
    window.scrollTo({
    top: 0
    });
    // or, window.scroll(0,0);
    }
    }





    share|improve this answer





















    • let me know if it works
      – Mahi
      Nov 19 '18 at 6:13



















    2














    This solution works perfectly in my project:



    export class AppComponent  {
    constructor (
    private _router: Router,
    ) {
    this._subscribeRouteEvents();
    }

    private _subscribeRouteEvents (): void {
    this._router.events.subscribe(e => {
    if (!(e instanceof NavigationEnd)) return;
    window.scrollTo(0, 0);
    });
    }
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53182560%2fangular-2-scroll-to-top-on-route-change-not-working%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13





      +100









      The router will emit an event when a new component gets loaded in the <router-outlet> so you can attach an event to it.



      So in your component with <router-outlet> use:



      <router-outlet (activate)="scrollTop($event)">



      and then in the same component where you placed <router-outlet> add the following method:



      scrollTop(event) {
      window.scroll(0,0);
      }





      share|improve this answer





















      • I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
        – user979331
        Nov 14 '18 at 16:42










      • Actually, this does work, but only when I remove height: 100% to the body
        – user979331
        Nov 14 '18 at 16:46










      • This answer appears to be correct, gonna do some testing on it first.
        – user979331
        Nov 14 '18 at 16:46
















      13





      +100









      The router will emit an event when a new component gets loaded in the <router-outlet> so you can attach an event to it.



      So in your component with <router-outlet> use:



      <router-outlet (activate)="scrollTop($event)">



      and then in the same component where you placed <router-outlet> add the following method:



      scrollTop(event) {
      window.scroll(0,0);
      }





      share|improve this answer





















      • I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
        – user979331
        Nov 14 '18 at 16:42










      • Actually, this does work, but only when I remove height: 100% to the body
        – user979331
        Nov 14 '18 at 16:46










      • This answer appears to be correct, gonna do some testing on it first.
        – user979331
        Nov 14 '18 at 16:46














      13





      +100







      13





      +100



      13




      +100




      The router will emit an event when a new component gets loaded in the <router-outlet> so you can attach an event to it.



      So in your component with <router-outlet> use:



      <router-outlet (activate)="scrollTop($event)">



      and then in the same component where you placed <router-outlet> add the following method:



      scrollTop(event) {
      window.scroll(0,0);
      }





      share|improve this answer












      The router will emit an event when a new component gets loaded in the <router-outlet> so you can attach an event to it.



      So in your component with <router-outlet> use:



      <router-outlet (activate)="scrollTop($event)">



      and then in the same component where you placed <router-outlet> add the following method:



      scrollTop(event) {
      window.scroll(0,0);
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 14 '18 at 15:52









      Mac_WMac_W

      815415




      815415












      • I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
        – user979331
        Nov 14 '18 at 16:42










      • Actually, this does work, but only when I remove height: 100% to the body
        – user979331
        Nov 14 '18 at 16:46










      • This answer appears to be correct, gonna do some testing on it first.
        – user979331
        Nov 14 '18 at 16:46


















      • I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
        – user979331
        Nov 14 '18 at 16:42










      • Actually, this does work, but only when I remove height: 100% to the body
        – user979331
        Nov 14 '18 at 16:46










      • This answer appears to be correct, gonna do some testing on it first.
        – user979331
        Nov 14 '18 at 16:46
















      I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
      – user979331
      Nov 14 '18 at 16:42




      I tried this, scrollTop is being called on every page, but its not scrolling to the top of the page.
      – user979331
      Nov 14 '18 at 16:42












      Actually, this does work, but only when I remove height: 100% to the body
      – user979331
      Nov 14 '18 at 16:46




      Actually, this does work, but only when I remove height: 100% to the body
      – user979331
      Nov 14 '18 at 16:46












      This answer appears to be correct, gonna do some testing on it first.
      – user979331
      Nov 14 '18 at 16:46




      This answer appears to be correct, gonna do some testing on it first.
      – user979331
      Nov 14 '18 at 16:46













      2














      Wait for the component to initialized component before you start scrolling. So better to put this code under ngAfterViewInit function.



       ngAfterViewInit() {
      this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
      return;
      }
      window.scrollTo(0, 0)
      });
      }





      share|improve this answer





















      • Trying this now.
        – user979331
        Nov 7 '18 at 19:26










      • Nope this does not work
        – user979331
        Nov 7 '18 at 19:28










      • Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
        – Sunil Singh
        Nov 7 '18 at 19:29










      • I put a console.log right before window.scrollTo(0, 0) and it is not being executed
        – user979331
        Nov 7 '18 at 20:45










      • Wait my bad, the console log is getting executed.
        – user979331
        Nov 7 '18 at 20:47
















      2














      Wait for the component to initialized component before you start scrolling. So better to put this code under ngAfterViewInit function.



       ngAfterViewInit() {
      this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
      return;
      }
      window.scrollTo(0, 0)
      });
      }





      share|improve this answer





















      • Trying this now.
        – user979331
        Nov 7 '18 at 19:26










      • Nope this does not work
        – user979331
        Nov 7 '18 at 19:28










      • Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
        – Sunil Singh
        Nov 7 '18 at 19:29










      • I put a console.log right before window.scrollTo(0, 0) and it is not being executed
        – user979331
        Nov 7 '18 at 20:45










      • Wait my bad, the console log is getting executed.
        – user979331
        Nov 7 '18 at 20:47














      2












      2








      2






      Wait for the component to initialized component before you start scrolling. So better to put this code under ngAfterViewInit function.



       ngAfterViewInit() {
      this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
      return;
      }
      window.scrollTo(0, 0)
      });
      }





      share|improve this answer












      Wait for the component to initialized component before you start scrolling. So better to put this code under ngAfterViewInit function.



       ngAfterViewInit() {
      this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
      return;
      }
      window.scrollTo(0, 0)
      });
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 7 '18 at 7:32









      Sunil SinghSunil Singh

      6,1472626




      6,1472626












      • Trying this now.
        – user979331
        Nov 7 '18 at 19:26










      • Nope this does not work
        – user979331
        Nov 7 '18 at 19:28










      • Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
        – Sunil Singh
        Nov 7 '18 at 19:29










      • I put a console.log right before window.scrollTo(0, 0) and it is not being executed
        – user979331
        Nov 7 '18 at 20:45










      • Wait my bad, the console log is getting executed.
        – user979331
        Nov 7 '18 at 20:47


















      • Trying this now.
        – user979331
        Nov 7 '18 at 19:26










      • Nope this does not work
        – user979331
        Nov 7 '18 at 19:28










      • Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
        – Sunil Singh
        Nov 7 '18 at 19:29










      • I put a console.log right before window.scrollTo(0, 0) and it is not being executed
        – user979331
        Nov 7 '18 at 20:45










      • Wait my bad, the console log is getting executed.
        – user979331
        Nov 7 '18 at 20:47
















      Trying this now.
      – user979331
      Nov 7 '18 at 19:26




      Trying this now.
      – user979331
      Nov 7 '18 at 19:26












      Nope this does not work
      – user979331
      Nov 7 '18 at 19:28




      Nope this does not work
      – user979331
      Nov 7 '18 at 19:28












      Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
      – Sunil Singh
      Nov 7 '18 at 19:29




      Put the logs before window.scrollTo(0, 0) and check if getting executed. I am doubting return statement is getting executed.
      – Sunil Singh
      Nov 7 '18 at 19:29












      I put a console.log right before window.scrollTo(0, 0) and it is not being executed
      – user979331
      Nov 7 '18 at 20:45




      I put a console.log right before window.scrollTo(0, 0) and it is not being executed
      – user979331
      Nov 7 '18 at 20:45












      Wait my bad, the console log is getting executed.
      – user979331
      Nov 7 '18 at 20:47




      Wait my bad, the console log is getting executed.
      – user979331
      Nov 7 '18 at 20:47











      2














      This similar issue was faced by me which was due to the style applied to body.
      i.e.



      body {
      height: 100%;
      overflow-x: hidden;
      }


      If I removed this style then my layout was badly affected.



      Instead of removing style I tried below solution and it worked for me...



      Solution:



      export class AppComponent implements OnInit {


      constructor(private router: Router, private changeDetect: ChangeDetectorRef) {
      }

      ngOnInit() {
      this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
      return;
      }
      // Change height:100% into auto
      $('body').css('height', 'auto');
      // Successfully scroll back to top
      $('body').scrollTop(0);
      // Remove javascript added styles
      $('body').css('height', '');
      this.changeDetect.detectChanges();
      });

      }
      }





      share|improve this answer




























        2














        This similar issue was faced by me which was due to the style applied to body.
        i.e.



        body {
        height: 100%;
        overflow-x: hidden;
        }


        If I removed this style then my layout was badly affected.



        Instead of removing style I tried below solution and it worked for me...



        Solution:



        export class AppComponent implements OnInit {


        constructor(private router: Router, private changeDetect: ChangeDetectorRef) {
        }

        ngOnInit() {
        this.router.events.subscribe((evt) => {
        if (!(evt instanceof NavigationEnd)) {
        return;
        }
        // Change height:100% into auto
        $('body').css('height', 'auto');
        // Successfully scroll back to top
        $('body').scrollTop(0);
        // Remove javascript added styles
        $('body').css('height', '');
        this.changeDetect.detectChanges();
        });

        }
        }





        share|improve this answer


























          2












          2








          2






          This similar issue was faced by me which was due to the style applied to body.
          i.e.



          body {
          height: 100%;
          overflow-x: hidden;
          }


          If I removed this style then my layout was badly affected.



          Instead of removing style I tried below solution and it worked for me...



          Solution:



          export class AppComponent implements OnInit {


          constructor(private router: Router, private changeDetect: ChangeDetectorRef) {
          }

          ngOnInit() {
          this.router.events.subscribe((evt) => {
          if (!(evt instanceof NavigationEnd)) {
          return;
          }
          // Change height:100% into auto
          $('body').css('height', 'auto');
          // Successfully scroll back to top
          $('body').scrollTop(0);
          // Remove javascript added styles
          $('body').css('height', '');
          this.changeDetect.detectChanges();
          });

          }
          }





          share|improve this answer














          This similar issue was faced by me which was due to the style applied to body.
          i.e.



          body {
          height: 100%;
          overflow-x: hidden;
          }


          If I removed this style then my layout was badly affected.



          Instead of removing style I tried below solution and it worked for me...



          Solution:



          export class AppComponent implements OnInit {


          constructor(private router: Router, private changeDetect: ChangeDetectorRef) {
          }

          ngOnInit() {
          this.router.events.subscribe((evt) => {
          if (!(evt instanceof NavigationEnd)) {
          return;
          }
          // Change height:100% into auto
          $('body').css('height', 'auto');
          // Successfully scroll back to top
          $('body').scrollTop(0);
          // Remove javascript added styles
          $('body').css('height', '');
          this.changeDetect.detectChanges();
          });

          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 14:31

























          answered Nov 14 '18 at 7:34









          khushkhush

          506214




          506214























              2














                constructor(
              private router: Router,
              private ngZone: NgZone) {
              router.events.subscribe((event: RouterEvent) => {
              this._navigationInterceptor(event);
              });
              }

              private _navigationInterceptor(event: RouterEvent): void {
              if (event instanceof NavigationStart) {
              }
              if (event instanceof NavigationEnd) {
              window.scrollTo({
              top: 0
              });
              // or, window.scroll(0,0);
              }
              }





              share|improve this answer





















              • let me know if it works
                – Mahi
                Nov 19 '18 at 6:13
















              2














                constructor(
              private router: Router,
              private ngZone: NgZone) {
              router.events.subscribe((event: RouterEvent) => {
              this._navigationInterceptor(event);
              });
              }

              private _navigationInterceptor(event: RouterEvent): void {
              if (event instanceof NavigationStart) {
              }
              if (event instanceof NavigationEnd) {
              window.scrollTo({
              top: 0
              });
              // or, window.scroll(0,0);
              }
              }





              share|improve this answer





















              • let me know if it works
                – Mahi
                Nov 19 '18 at 6:13














              2












              2








              2






                constructor(
              private router: Router,
              private ngZone: NgZone) {
              router.events.subscribe((event: RouterEvent) => {
              this._navigationInterceptor(event);
              });
              }

              private _navigationInterceptor(event: RouterEvent): void {
              if (event instanceof NavigationStart) {
              }
              if (event instanceof NavigationEnd) {
              window.scrollTo({
              top: 0
              });
              // or, window.scroll(0,0);
              }
              }





              share|improve this answer












                constructor(
              private router: Router,
              private ngZone: NgZone) {
              router.events.subscribe((event: RouterEvent) => {
              this._navigationInterceptor(event);
              });
              }

              private _navigationInterceptor(event: RouterEvent): void {
              if (event instanceof NavigationStart) {
              }
              if (event instanceof NavigationEnd) {
              window.scrollTo({
              top: 0
              });
              // or, window.scroll(0,0);
              }
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 18 '18 at 16:20









              MahiMahi

              707319




              707319












              • let me know if it works
                – Mahi
                Nov 19 '18 at 6:13


















              • let me know if it works
                – Mahi
                Nov 19 '18 at 6:13
















              let me know if it works
              – Mahi
              Nov 19 '18 at 6:13




              let me know if it works
              – Mahi
              Nov 19 '18 at 6:13











              2














              This solution works perfectly in my project:



              export class AppComponent  {
              constructor (
              private _router: Router,
              ) {
              this._subscribeRouteEvents();
              }

              private _subscribeRouteEvents (): void {
              this._router.events.subscribe(e => {
              if (!(e instanceof NavigationEnd)) return;
              window.scrollTo(0, 0);
              });
              }
              }





              share|improve this answer


























                2














                This solution works perfectly in my project:



                export class AppComponent  {
                constructor (
                private _router: Router,
                ) {
                this._subscribeRouteEvents();
                }

                private _subscribeRouteEvents (): void {
                this._router.events.subscribe(e => {
                if (!(e instanceof NavigationEnd)) return;
                window.scrollTo(0, 0);
                });
                }
                }





                share|improve this answer
























                  2












                  2








                  2






                  This solution works perfectly in my project:



                  export class AppComponent  {
                  constructor (
                  private _router: Router,
                  ) {
                  this._subscribeRouteEvents();
                  }

                  private _subscribeRouteEvents (): void {
                  this._router.events.subscribe(e => {
                  if (!(e instanceof NavigationEnd)) return;
                  window.scrollTo(0, 0);
                  });
                  }
                  }





                  share|improve this answer












                  This solution works perfectly in my project:



                  export class AppComponent  {
                  constructor (
                  private _router: Router,
                  ) {
                  this._subscribeRouteEvents();
                  }

                  private _subscribeRouteEvents (): void {
                  this._router.events.subscribe(e => {
                  if (!(e instanceof NavigationEnd)) return;
                  window.scrollTo(0, 0);
                  });
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 10:29









                  Eugene MihaylinEugene Mihaylin

                  9581424




                  9581424






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53182560%2fangular-2-scroll-to-top-on-route-change-not-working%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      The Sandy Post

                      Danny Elfman

                      Pages that link to "Head v. Amoskeag Manufacturing Co."