• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

149
Views
Displaying No Records Found in Angular 2

I have been searching and wondering if there is any directive to help display " No data found " in a table when the table is empty in angular 2. Or better still i can create that functionality in my services when i am subscribing to a fetched data ?

  <table>
    <tbody>
        <tr *ngFor="let game of Games">

         </td>
          <td>{{game.name}}</td>
          <td>{{game.type}}</td>
          </tr>


      </tbody>
</table>
almost 3 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use the newest feature from Angular 4.0.0 and to add if else statement:

<div *ngIf="Games?.length;else no_data_templ">
    <table>
        <tbody>
           <tr *ngFor="let game of Games">
                <td>{{game.name}}</td>
                <td>{{game.type}}</td>
            </tr>
        </tbody>
    </table>
</div>

<ng-template #no_data_templ>
     No daata found...
</ng-template>

Update: for Angular 2.X you can use the following approach:

<div *ngIf="Games?.length">
    <table>
        <tbody>
           <tr *ngFor="let game of Games">
                <td>{{game.name}}</td>
                <td>{{game.type}}</td>
            </tr>
        </tbody>
    </table>
</div>

<div *ngIf="! Games?.length">
     No data found...
</div>
almost 3 years ago · Santiago Trujillo Report

0

Check for the length of array if there are no elements uisng length, then display it as NO DATA,

<li  *ngIf="Games?.length == 0">
    <span class="search_no_results">
       No data found 
    </span>
</li>
almost 3 years ago · Santiago Trujillo Report

0

If you want to display a message saying no data found. Before the table tag check whether Games has items to iterate.

Something like this

public hasData(): boolean {
 return (this.Games != null && this.Games.length > 0);
}

Use hasData() in the template

<div *ngIf="!hasData()">No Data Found</div>
<table *ngIf="hasData()">
    <tbody>
        <tr *ngFor="let game of Games">

         </td>
          <td>{{game.name}}</td>
          <td>{{game.type}}</td>
          </tr>


      </tbody>
</table>

You can structure & style this in anyway you want.

almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error