[Fixed] Conditionally add RouterLink or other attribute directives to an element in Angular 2


In Angular 2 if I have an element like <button></button> how can I conditionally add an attribute directive like [routerLink]="['SomeRoute'] to it?


As far as I know, there is no straight way to do this. There are some workarounds… I used something like this:

<button *ngIf="condition" [routerLink]="['SomeRoute']"></button>
<button *ngIf="!condition"></button>

There is an similar discussion here: link

Leave a Reply

(*) Required, Your email will not be published