Search code examples
angularangular-ng-if

How do I use the *ngIf then else with relational operators?


What is the correct way of doing a ngIf then else when you have a count involved? Please refer to the example below for more info:

<div *ngIf={{ returnedTotalResults }} > 0 then #paging;else #no_data_found></div>
<pagination #paging></pagination>
<p #no_data_found>No results found</p>

Solution

  • You need to put your ngIf as a string, some some tags around and remove the string interpolation:

        <div *ngIf=" returnedTotalResults > 0; else no_data_found">
            <pagination></pagination>
        </div>
        <ng-template #no_data_found>
            <p>No results found</p>
        </ng-template>
    

    https://angular.io/api/common/NgIf