Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ngFor isn't working on <tr> element of my angular project

So I’m working on a demo CRUD project and I requested the sections from my component where I hard-coded employees data. I am not able to receive the data in table rows. I don’t see what’s making this not work.

Component Html template (employees-list.component.html)

    <div class="container my-5">
      <h1 class="mb-5">Employees</h1>
    
      <table class="table" *ngIf="employees.length > 0">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Salary</th>
            <th>Department</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let emp from employees">
            <td>{{emp.id}}</td>
            <td>{{emp.name}}</td>
            <td>{{emp.email}}</td>
            <td>{{emp.phone}}</td>
            <td>{{emp.salary}}</td>
            <td>{{emp.department}}</td>
    
          </tr>
        </tbody>
      </table>
    
      <p *ngIf="employees.length <= 0">No employees found</p>
    </div>

Component ts template (employees-list.component.ts)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    import { Employee } from './../../../models/employee.model';
    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-employees-list',
      templateUrl: './employees-list.component.html',
      styleUrls: ['./employees-list.component.css']
    })
    export class EmployeesListComponent implements OnInit {
    
      employees: Employee[] = [
        {
          id: 1,
          name: 'John Doe',
          email: 'john.doe@mail.com',
          phone: 1234567890,
          salary: 100000,
          department: 'IT'
        },
        {
          id: 2,
          name: 'Bob Builder',
          email: 'bob.build@mail.com',
          phone: 2345678901,
          salary: 200000,
          department: 'HR'
        },
        {
          id: 3,
          name: 'Phineas Ferb',
          email: 'phineas.ferb@mail.com',
          phone: 3456789012,
          salary: 300000,
          department: 'Scientist'
        },
        {
          id: 4,
          name: 'Nobita Nobi',
          email: 'nobita.nobi@mail.com',
          phone: 4567890123,
          salary: 400000,
          department: 'Marketing'
        }
      ];
      constructor() { }
    
      ngOnInit(): void {
      }
    
    }

App module file (app.module.ts)

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { EmployeesListComponent } from './components/employees/employees-list/employees-list.component';
    import { HttpClientModule } from '@angular/common/http';
    
    @NgModule({
      declarations: [
        AppComponent,
        EmployeesListComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        HttpClientModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

Here below you can see that I am not able to get data in table rows

enter image description here

>Solution :

Instead, it should be *ngFor with of.

<tr *ngFor="let emp of employee">
  ...
</tr>

Demo @ StackBlitz

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading