Adding routerLink to anchor tag dynamically based on if a router link is passed to component

Discussão em 'Angular' iniciado por StephenRios, Junho 21, 2025.

  1. StephenRios

    StephenRios Guest

    I am trying to add routerLink to an anchor tag dynamically based on if a routerLink is passed to the component itself.

    This is the code I currently have:

    [attr.routerLink]="router ? router : null"


    The resulting markup looks like this when a routerLink is passed, but no href is passed:

    <a _ngcontent-hlo-122="" routerlink="/myRoute">My Link</a>


    I've tried this out a number of ways, but I can never seem to get this to work. Does anyone have a solid solution to this problem?

    Continue reading...

Compartilhe esta Página