0

How would I implement "ngif" to a routerlink instead of creating a button?

example: *ngIf="auth.isAuthenticated()" (click)="auth.logout()"

To: <a [routerLink]="['/']">Logout

Mystikal79
  • 3
  • 1
  • 3
  • 2
    What's the purpose of combining `*ngIf` with `routerLink`. Sounds weird to me. How is the button related? – Günter Zöchbauer Feb 16 '18 at 07:08
  • I'm using Auth0 for authentications and I'm using a template that already has a routerlink logout link. Instead of creating an actually button, I want to implement into the routerlink. Example of login button that came from Auth0's guide ' – Mystikal79 Feb 16 '18 at 15:26
  • Ouch, I mixed up `routerLink` with `router-outlet`. So what is actually the problem with `routerLink` and `*ngIf`? Btw. calling a function in view bindings like `*ngIf="auth.isAuthenticated()"` is prone to cause serious performance issues if `auth.isAuthenticated()` is doing some actual work (instead of perhaps just returning a value from a field). `auth.isAuthenticated` will be called very frequently (every time Angular runs change detection). It would be better to use an `Observable` that emits the new value when the value changed. – Günter Zöchbauer Feb 16 '18 at 15:32
  • event bindings like `(click)="auth.logout()"` are perfectly fine of course – Günter Zöchbauer Feb 16 '18 at 15:34

2 Answers2

0

You can put ngIf on any element. In your case <a *ngIf="auth.isAuthenticated()" [routerLink]="['/']">Logout</a> should work fine.

jaibatrik
  • 6,770
  • 9
  • 33
  • 62
0

Try this

<span  *ngIf="auth.isAuthenticated()"><a [routerLink]="['/']">Logout</a></span>
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234