Search code examples
htmlpathhrefrouterlink

using href or routerlink with #


I'm fairly new to changing paths / position of page so I would like a little help on this.

Say, when a button is clicked, I want to scroll down to another portion of the page. (where id of that section is 'xyz') however, I'm using an entirely different component to access that section.

If I were to use href, I can easily do : href="/app/appid#xyz" however, if appid is a variable retrieved from the ts file, how can I insert it into my href?

It's easier to to use [routerlink]="['app', appid]" but how can I insert the "#xyz" into my string?

Or is there a completely separate and easier functionality I can use?

Thank you


Solution

  • Add the frament attribute:

    <a [routerLink]="['app', appid] fragment="xyz">Link</a>
    

    https://angular.io/api/router/RouterLink