ng if ng template code example

Example 1: ng if else

<div *ngIf="condition; else elseBlock">Content to render when condition is true.</div>
<ng-template #elseBlock>Content to render when condition is false.</ng-template>

Example 2: ngIf else

content_copy
      
      <div *ngIf="condition; then thenBlock else elseBlock"></div>
<ng-template #thenBlock>Content to render when condition is true.</ng-template>
<ng-template #elseBlock>Content to render when condition is false.</ng-template>

Example 3: ngTemplate

<div *ngIf='name && lastName; then thereAreParams else thereArentAnyParams'>

</div>

<ng-template #thereAreParams>
  <h2>Name is : {{name}} </h2>
   <h2>Last name is : {{lastName}} </h2>
</ng-template>

<ng-template #thereArentAnyParams>
  <h2>There aren't any parameters</h2>
</ng-template>