Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5: calling method from html

Tags:

angular

I am not getting why it is not working.

I am trying to show Login/Logout link based on condtion by calling a method from html. in console I can see it is returning "true", but it never changes the link to "Logout". it is always "Login" on the screen

<ul class="nav navbar-nav navbar-right">
    <li class="nav-item dropdown" [hidden]="!authenticated()">
        <a class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-user" aria-hidden="true"></i> Welcome, {{user}}!
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <a class="nav-link dropdown-item" (click)="logout()">Logout</a>
        </div>
    </li>
    <li class="nav-item dropdown" [hidden]="authenticated()">
        <a routerLinkActive="active" routerLink="/login">Login</a>
    </li>
</ul>

component.ts

authenticated() {
    console.log(localStorage.getItem('authenticated'));
    if(localStorage.getItem('authenticated') === 'true') {
        console.log("YES");
        return true;
    } else {
        console.log("NO");
        return false;    
    }
}

Before clicking on Login link enter image description here

After click on Login enter image description here

After entering correct username/password (Her you can see in console it returns true) enter image description here

like image 270
SK. Avatar asked Sep 21 '26 05:09

SK.


2 Answers

First of all you should not call a function with *ngIf directive, as it will run with every change detection. This can lead to performance overhead. Either use a variable or convert your method into a getter property e.g.

get authenticated(): boolean {
    return localStorage.getItem('authenticated') === true;
}

Secondly, use *ngIf instead of [hidden]:

<ul class="nav navbar-nav navbar-right">
    <li class="nav-item dropdown" *ngIf="authenticated">
        <a class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-user" aria-hidden="true"></i> Welcome, {{user}}!
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <a class="nav-link dropdown-item" (click)="logout()">Logout</a>
        </div>
    </li>
    <li class="nav-item dropdown" *ngIf="!authenticated">
        <a routerLinkActive="active" routerLink="/login">Login</a>
    </li>
</ul>
like image 124
Faisal Avatar answered Sep 22 '26 19:09

Faisal


Replace hidden directive with *ngIf directive.

<ul class="nav navbar-nav navbar-right">
    <li class="nav-item dropdown" *ngIf="!authenticated()">
        <a class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-user" aria-hidden="true"></i> Welcome, {{user}}!
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <a class="nav-link dropdown-item" (click)="logout()">Logout</a>
        </div>
    </li>
    <li class="nav-item dropdown" *ngIf="authenticated()">
        <a routerLinkActive="active" routerLink="/login">Login</a>
    </li>
</ul>

It's functinally the same, but ng-if directive remove the element from DOM, and hidden attribute just hides it.

Another solution is to change your method to this:

authenticated() {
    console.log(localStorage.getItem('authenticated'));
    if(localStorage.getItem('authenticated') === 'true') {
        console.log("YES");
        return true;
    } else {
        console.log("NO");
        return null;    
    }
}
like image 45
Roberto Zvjerković Avatar answered Sep 22 '26 19:09

Roberto Zvjerković