Angular 7-使用* ngFor显示数据问题(数据未显示)

时间:2019-09-15 08:37:26

标签: angular ngfor

我有下面的Json示例,我试图在* ngFor中绑定它:

[
    {
        "employeeId": 1,
        "name": "johnny",
        "dob": "4/20/1992 12:00:00 AM",
        "salary": "100"
    },
    {
        "employeeId": 2,
        "name": "test",
        "dob": "10/10/2000 12:00:00 AM",
        "salary": "100"
    },
    {
        "employeeId": 3,
        "name": "Johnny Rahme",
        "dob": "1/10/2001 12:00:00 AM",
        "salary": "100"
    }
]

在我的app.component.html上,我有这个:

<table class='table'>
<thead>
  <tr>
    <th>EmployeeId</th>
    <th>Name</th>
    <th>DOB</th>
    <th>Salary</th>
  </tr>
</thead>
<tbody>
  <tr *ngFor="let emp of empList">
    <td>{{ emp.EmployeeId }}</td>
    <td>{{ emp.Name }}</td>
    <td>{{ emp.DOB }}</td>
    <td>{{ emp.Salary }}</td>
    <td>
  </tr>
</tbody>
</table>

在我的app.component.ts上,我拥有:

this.empList = data // that contains the data from an api 

我该如何解决?

1 个答案:

答案 0 :(得分:0)

tempalte中的属性名称与组件中的对象不同。请注意,模板中属性名称的首字母均为大写,而组件中的所有字母均为小写。请使用以下内容更改模板。

<table class='table'>
    <thead>
        <tr>
            <th>EmployeeId</th>
            <th>Name</th>
            <th>DOB</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let emp of empList">
            <td>{{ emp.employeeId }}</td>
            <td>{{ emp.name }}</td>
            <td>{{ emp.dob}}</td>
            <td>{{ emp.salary }}</td>
            <td>
        </tr>
    </tbody>
</table>

Demo