I'm currently developing a search screen for an application and I've come up with three possible outcomes for the results section.
- If the user hasn't searched yet, then show nothing.
- If the user searches and the array is empty, display "no results".
- If the user searches and the array isn't empty, loop through and display the results.
I've managed to make it work fine for empty vs non-empty arrays:
<ng-container *ngIf="results.length">
<div class="result" *ngFor="let result of results">
<a href="link-goes-here">Open Result</a>
<search-result [result]="result"></search-result>
</div>
</ng-container>
<ng-container #elseBlock>
No Results found.
</ng-container>
However, when I try to include a check for results
being undefined
, it doesn't seem to work as expected. I attempted to use [ngSwitch]
, but that also failed. Alternatively, I could create a boolean variable starting as false, then set to true after the first search, but I prefer having my array start as undefined and then get assigned after the initial search.