In my Angular application I have a <form>
with a <select>
field whose options are fetched at runtime from the server. While the HTTP request is pending I show a loading icon placeholder using ngIf/else:
<div class="form-group row">
<label for="nameInput" class="col-3 col-form-label-sm">Name</label>
<div class="col">
<select *ngIf="(listOfNames$ | async) as listOfNames; else loading"
id="nameInput" class="form-control form-control-sm" formControlName="name">
<option *ngFor="let option of listOfNames" [value]="option">{{ option }}</option>
</select>
</div>
</div>
<ng-template #loading>
<div class="form-control form-control-sm">
<div class="loading-spinner-anim"></div>
</div>
</ng-template>
This works great except that it's a common use case for the Observable
listOfNames$
to resolve to an empty array and when this happens it shows the #loading
template indefinitely because in the *ngIf
directive an empty array is falsy so the else is rendered.
How can I rewrite this to show "loading" only while the Observable
listOfNames$
is un-completed and to show something else if it resolves to an empty array?
Thanks in advance!