Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional (if, else) in Angular 2

I worked a few years in .NET, Silverlight now I'm starting with Angular 2 and Expressjs. And I have a doubt that even I could not find how can I do this in angular 2 + Expressjs, and is safe from client side?

<% if(User.Identity.IsAuthenticated){ %>
     <b>Sign Out</b>
<% } else { %>
     <b>Sign In</b>
<% } %>
like image 206
Raimens Avatar asked Aug 09 '16 13:08

Raimens


People also ask

Can we have two ngIf in Angular?

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.

Why * is used in ngIf?

The asterisk is "syntactic sugar". It simplifies ngIf and ngFor for both the writer and the reader. Under the hood, Angular replaces the asterisk version with a more verbose form.

Can we use ngIf and ngFor together?

Use ngFor and ngIf on same element It's very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.


2 Answers

If you need the "else", perhaps the best way is to use NgSwitch (https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html)

So something like:

<div [ngSwitch]="User?.Identity?.IsAuthenticated">
  <b *ngSwitchCase="true">Sign Out</b>
  <b *ngSwitchDefault>Sign In</b>
</div>
like image 121
Sierrodc Avatar answered Oct 04 '22 22:10

Sierrodc


With Angular 4, this is now possible:

  • SYNCHRONOUSLY

    lets assume that we've defined this class variable:

    shown: boolean = true;
    

    If else syntax:

    <button (click)="shown = !shown">toggle 'shown' variable</button>
    <div *ngIf="shown; else elseBlock">If shown</div>
    <ng-template #elseBlock>else not shown</ng-template>
    

    Note that ng-template must be used in order for this structural directive to work, so if you have a custom component, wrap it inside ng-template. Below is an alternative syntax which also binds to the same class variable.

    If then else syntax:

    <button (click)="shown = !shown">toggle 'shown' variable</button>
    <div *ngIf="shown; then primaryBlock; else secondaryBlock"></div>
    <ng-template #primaryBlock>If shown</ng-template>
    <ng-template #secondaryBlock>else not shown</ng-template>
    

  • ASYNCHRONOUSLY

    class:

    userObservable = new Subject<{first: string, last: string}>();
    onButtonClick() {
      this.userObservable.next({first: 'John', last: 'Smith'});
    }
    

    template:

    <button (click)="onButtonClick()">Display User</button>
    <div *ngIf="userObservable | async as user; else loading">
      Hello {{user.last}}, {{user.first}}!
    </div>
    <ng-template #loading>Waiting for click...</ng-template>
    
like image 25
Stephen Paul Avatar answered Oct 04 '22 22:10

Stephen Paul