• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use *ngIf else?
#1
I'm using Angular and I want to use *ngIf else (available since version 4) in this example:
Code:
<div *ngIf="isValid">
  content here ...
</div>

<div *ngIf="!isValid">
other content here...
</div>
How can I acheive the same behavior with *ngIf else?
  Reply
#2
ANGULAR 4 and 5 using else:
Code:
<div *ngIf="isValid;else other_content">
    content here ...
</div>

<ng-template #other_content>other content here...</ng-template>
you can also use then else:
Code:
<div *ngIf="isValid;then content else other_content">here is ignored</div>   
<ng-template #content>content here...</ng-template>
<ng-template #other_content>other content here...</ng-template>
or then alone :
Code:
<div *ngIf="isValid;then content"></div>   
<ng-template #content>content here...</ng-template>
Demo: <ng-template>: is Angular’s own implementation of the <template>
 tag which is according to MDN :
The HTML <template>element is a mechanism for holding client-side content that is not to be rendered when a page is loaded but may subsequently be instantiated during runtime using JavaScript.
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)