In Angular 2 if I have an element like <button></button>
how can I conditionally add an attribute directive like [routerLink]="['SomeRoute']
to it?
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
Or you can simply add a condition to the attribute.
<button [routerLink]="myVar ? ['/myScreen'] : []"></button>
Redirect to '/myScreen' only if myVar is true.
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
If you don't want to duplicate the element, and just want to prevent clicks depending on the condition, you could do the following:
<button
[style.pointer-events]="condition ? 'auto' : 'none'"
routerLink="/some/route"
>
</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With