1.

How would you simulate if-then-else blocks in the templates of Angular components?

Answer»

The ngIf directive gets a nice improvement in Angular version 4.0.0.  Earlier, in order to simulate if-then-else blocks in Angular templates, we had to use two ngIf DIRECTIVES with opposed boolean CONDITIONS. In Angular version 4 and HIGHER we now get an “else” instruction as part of the ngIf directive. Here is an example.

<div *ngIf="isLoggedIn(); else notLoggedIn">      Hi, {{ user.NAME }}! </div> <ng-template #notLoggedIn>      You're not logged in. </ng-template>

In the above code, the else keyword is used to specify the name of a template variable that will be used to render a template only if the if-condition fails to match. SIMPLE, right?



Discussion

No Comment Found