Angular – How to display single "no results" message on no results


I’m having trouble coming up with a way to show my "no results" div element. Basically, I have a list component containg order timeline section components, each one of these section contains order components. Like so:

enter image description here

My orders-list.component.html (check bottom div):

<div class="list-container" [ngClass]="{section: isDeliverySlotsActive === false}">
    <label class="list-header" *ngIf="isDeliverySlotsActive === true" style="margin-top: 1.625rem">DELIVERY SLOTS ORDERS</label>
    <div [ngClass]="{section: isDeliverySlotsActive === true}" *ngFor="let date of timelines">
/* I want to show the below div when there are no results for the search */
<div id="no-results">
    <img src="../../../assets/my-orders/no-results.png" alt="No Results" style="margin-top: 6.063rem; margin-bottom: 2.837rem;">
    <label class="no-results-text">COULDN'T FIND ANYTHING</label>
    <label class="no-results-text weight-medium">Search by order number or customer</label>

For each section, a filtering method is applied when the user searches for an order using the search bar. If the search term does not correspond to an order in a section, the order is not displayed for that section. If there are no results for that section the section header is also not displayed.

My orders-list-section.component.html:

<div *ngIf="filteredSectionOrders.length > 0">
        *ngIf="isDeliverySlotsActive === true"
        [ngClass]="{ slots: isDeliverySlotsActive === true }">
    {{ timeline | addSectionDateFormat }}
<div *ngFor="let order of filteredSectionOrders">

My filter method in the section component:

filterSectionOrders(searchString: string){
    if(!searchString) return;
        this.filteredSectionOrders = this.filteredSectionOrders.filter(order => order.order_num.toString().indexOf(searchString) !== -1);
        this.filteredSectionOrders = this.filteredSectionOrders.filter(order => {
            if(order.first_name && order.last_name){
                let fullName = order.first_name + " " + order.last_name;
                if(fullName.toLowerCase().indexOf(searchString.toLowerCase()) !== -1){
                    return order;

Given that I apply this filter to each section and not to the list as a whole, how can I find out when there are 0 total results so I can show only one (not for each section) div element with a "no results found" message?

Thanks in advance.

Source: Angular Questions

Categorized as angular, html, typescript Tagged , ,


You can easily use *ngIf;else link to ngIf from angular inside your HTML

I am not sure where do you use filteredSectionOrders, because it is not shown in your html, but let’s assume your app-orders-list-section has some HTML logic where you use *ngFor to loop through orders and show it properly

so, I guess your code looks something like this

<div class="order" *ngFor="let order of filteredSectionOrders">
    {{ order.first_name + ' ' + order.last_name }}

This is simplified html how I assume it looks like.

What you can do is next:

<ng-template *ngIf="filteredSectionOrders.length > 0; else noResultsBlock">
  // here you insert your code to render orders
  <div class="order" *ngFor="let order of filteredSectionOrders">
      {{ order.first_name + ' ' + order.last_name }}
<ng-template #noResultsBlock>
  <p> No results </p>

So, this would simple solution
If you want to improve it even more, it would be better to have a new variable, lets say areThereResults, which you will set to true or false, at the end of your method filterSectionOrders, based on filterSectionOrders.length. Then, you would use this new variable inside *ngIf check, instead of filterSectionOrders.length > 0.

Reason for using boolean variable instead of using actual array is detection changes, and will anguar re-render UI inside *ngIf. You can read more about it on Angular documentation, just search for detection changes.

Mrs. Alexandrea Bechtelar

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us