0

Is there any way to use if elseif else structure in Angular 2 template?

This is if else example:

[text]="company ? company.name : 'Select a company'"

I need add elseif to it.

pelcomppl
  • 575
  • 2
  • 6
  • 16

1 Answers1

2

Angular doesn't have if elseif else behaviour yet but you can achieve the same behaviour like below

<!-- showing number divisible by 2 -->
<div *ngIf="number_divide_by(2);else else_if_content1">
  Show result of if_content
</div>
<!-- Showing number divisible by 3 -->
<ng-template #else_if_content1>
  <span *ngIf="number_divide_by(3);else else_if_content2">
   Show result of else_if_content1</span>
</ng-template>
<!-- Showing number divisible by 5 -->
<ng-template #else_if_content2>
  <span *ngIf="number_divide_by(5);else else_if_content3">
   Show result of else_if_content2</span>
</ng-template>
<!-- Showing rest of the number -->
<ng-template #else_if_content1>
  <span>Show result of else_content</span>
</ng-template>

Hope this will resolve your effort.

thekaushal
  • 31
  • 1
  • This apparently doesn't work if the conditions past the first (else_if_contentX) use an if..then..else statement – Lindenk Oct 28 '19 at 17:39