Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2- using *ngIf with multiple conditions

Tags:

I'm unable to selectively display links on my nav-bar. Based on who logs in (user or admin), I want to change which link shows on my nav-bar.

Below is the code for one such instance where the user/admin logs out.

In navbar.component.html -

<li *ngIf="authService.userLoggedIn()== true && authService.adminLoggedIn() == false"         [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">    <a (click)="onUserLogoutClick()" href="#">Logout</a> </li>  <li *ngIf="(authService.adminLoggedIn() == true) && (authService.userLoggedIn() == false)"        [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">    <a (click)="onAdminLogoutClick()" href="#">Logout</a> </li> 

Both authService.adminLoggedIn() and authService.userLoggedIn() return tokenNotExpired;

Below is the relevant code in the navbar.component.ts -

 onUserLogoutClick() {    this.authService.userLogout();    this.flashMessage.show('You are now logged out', {cssClass: 'alert-success', timeout: 3000});    this.router.navigate(['/login']);    return false;     }   onAdminLogoutClick() {    this.authService.adminLogout();    this.flashMessage.show('Administrator now logged out', {cssClass: 'alert-success', timeout: 3000});    this.router.navigate(['/admin']);    return false;     } 

The authService.adminLogout() and authService.userLogout() just clears the token stored in local storage.

I apologize in advance if the mistake that I've made is silly. I'm new to Angular.

like image 855
Shakir Jameel Avatar asked Mar 25 '17 21:03

Shakir Jameel


People also ask

Can we use two conditions in ngIf?

We can use multiple conditions in *ngIf with logical operator AND (&&) to decide the trustworthy of *ngIf expression. If all conditions are true, then element will be added to the DOM.

What is * in * ngIf in Angular?

A shorthand form of the directive, *ngIf="condition" , is generally used, provided as an attribute of the anchor element for the inserted template. Angular expands this into a more explicit version, in which the anchor element is contained in an <ng-template> element.

What is * in * ngIf?

The * syntax means that ngIf is a structural directive, meaning that it affects the structure of the page.


2 Answers

You can move these multiline conditions and complex conditions to your component method as below

showLogout(){     if(this.authService.userLoggedIn()== true && this.authService.adminLoggedIn() == false)         return true;     else         return false; } 

Also, as the angular4 has *ngIf and else you can use it as

 <div *ngIf="showLogout();then userLogout else adminlogout"></div>  <ng-template #userLogout><a (click)="onUserLogoutClick()" href="#">Logout</a></li></ng-template> <ng-template #adminlogout><a (click)="onAdminLogoutClick()" href="#">Logout</a></li></ng-template> 
like image 131
Aravind Avatar answered Oct 20 '22 05:10

Aravind


authService.userLoggedIn() or authService.adminLoggedIn() in your expression wouldn't keep your template informed about who is logged in as your function is invoked only once.

Try make them getter in your service:

  get userLoggedIn(): boolean {     return this.who.user; // your logic   } 

Then in your template:

<li *ngIf="authService.userLoggedIn && !authService.adminLoggedIn"... 
like image 27
bob Avatar answered Oct 20 '22 06:10

bob