角度 7 - 使用 *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"
}
]

在我的应用程序组件上.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//包含来自 API 的数据

我该如何解决这个问题?

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>

演示

最新更新