3

I am using:

gotoAbout() { this.router.navigate(['/'], { fragment: 'about' }) } 

because routerLink and a href="#about" reloaded my page if I was not on my root / page. Using this function on the nav button it will navigate to /#about in one click without reloading the page but the problem is it is not jumping to that div at all like a href="#about" does when I am scrolled away from it on the page. Is there a way to achieve this in angular?

3
  • stackoverflow.com/questions/36201624/autoscroll-in-angular-2 Commented Feb 28, 2017 at 19:21
  • From the documentation: You can set query params and fragment as follows: <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education"> Commented Feb 28, 2017 at 19:24
  • I am trying to get to url.com/#about and jump to div id="about" from url.com/somethingelse/somethingelse. So I need a non-reloading router back to /#about and then the div jump. What does debug:true; do? Does it set ?debug=true in params or make it do a fragment jump after routing? Commented Feb 28, 2017 at 19:38

2 Answers 2

0

This seems a little hacky but it works:

In your component

goToDiv(fragment: string): void { window.location.hash = fragment; } 

In the template :

<a (click)="goToDiv('destination')">Go to div</a> <div id='destination'> <h2>Destination</h2> </div> 

Edit: As @JB Nizet stated in is comment you can also try :

<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education"> link to user component </a> 

https://angular.io/docs/ts/latest/api/router/index/RouterLink-directive.html

Sign up to request clarification or add additional context in comments.

3 Comments

Hacky is fine, but will this goToDiv work before my (click) router which takes me to /#about which causes my body page to display the about div, so the div is not rendered on the dom until the routing is finished
you can capture the fragment and run goToDiv() at Init. Edit: According to the doc, you can do better with something like this: this.router.navigate(['/results'], { fragment: 'top' });
I use this one component that reads the fragment from ActivatedRoute to determine what to display from amongst a couple options, this would work if I didnt do it that way :P Ill try your gotodiv solution now
0

For anyone interested, my solution goes as follows:

my navbar has a button 'about' which (click)="gotoAbout();" :

 gotoAbout() { let tree = this.router.parseUrl(this.router.url); if (tree.fragment == 'about') { let element = document.querySelector("#" + tree.fragment); if (element) { element.scrollIntoView(element); } } else { this.router.navigate(['/'], {fragment: 'about'}) .then( () => { let tree = this.router.parseUrl(this.router.url); let element = document.querySelector("#" + tree.fragment); if (element) { element.scrollIntoView(element); } }); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.