How can I use conditional [href] or [routerLink] on an anchor?


I need for the same anchor link to be pointed conditionally locally or to external resource. I tried

<a [href]="outside?externalUrl:null"  [routerLink]="outside?[]:['/route',id]" >test</a>

But it doesn't work. I don't get any errors, but it points to the same local page and ignores the external URL. Any ideas?

Another option would be to construct the link, but I can't find any docs how to access routerLink inside a service

Edit: I know I can clone the whole link with *ngIf but I don't want to do it, my link contains a video tag with a buch of options

The simplest way would be to use *ngIf / else:

<ng-container *ngIf="outside; else internal">

  <a [href]="externalUrl">External</a>

  <ng-template #internal>
    <a [routerLink]="['/route', id]">Internal</a>


EDIT#1: (Ugly workaround)

Since you're don't want to use *ngIf (I still don't understand why), you can do this:


<a href="javascript:void(0)" (click)="handleClick(outside, '/route', id, externalUrl)">Link</a>


public handleClick(outside, internal, internalId, externalUrl) {
  if (outside) {
    window.location.href = externalUrl;
    // You can also use Location class of Angular
  } else {