下面是员工详细信息数组,其中有对象,我想在表格行中显示员工详细信息:
this.employeedetails = [
{ id: 1, firstname: 'AM', lastname:'CM', age:3 },
{ id: 1, firstname: 'AM', lastname: 'DIM', age:4 },
{ id: 1, firstname: 'AM', lastname: 'FM', age:5 },
{ id: 1, firstname: 'AM', lastname: 'HM', age:6 }, ];
这就是我试图在html文件中打印数组的方式
{{employeedetails}}
答案 0 :(得分:2)
更正变量名称,例如:
this.employeeDetails = [
{ id: 1, firstName: 'AM', lastName:'CM', age:3 },
{ id: 1, firstName: 'AM', lastName: 'DIM', age:4 },
{ id: 1, firstName: 'AM', lastName: 'FM', age:5 },
{ id: 1, firstName: 'AM', lastName: 'HM', age:6 } ];
然后查看表格:
<table >
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let employee of employeeDetails ">
<th scope="row">{{ employee.id}}</th>
<td>{{ employee.firstName}}</td>
<td>{{ employee.lastName}}</td>
<td>{{ employee.age}}</td>
</tr>
</tbody>
</table>
答案 1 :(得分:0)
首先您的json无效,应该是
employeeDetails = [
{ id: 1, firstName: 'AM', lastName:'CM', age:3 },
{ id: 1, firstName: 'AM', lastName: 'DIM', age:4 },
{ id: 1, firstName: 'AM', lastName: 'FM', age:5 },
{ id: 1, firstName: 'AM', lastName: 'HM', age:6 }, ];
然后用类似HTML的用户*ngFor
<table>
<tr>
<td>First name</td>
<td>Last name</td>
</tr>
<tr *ngFor="let item of employeeDetails">
<td>{{item.firstName}}</td>
<td>{{item.lastName}}</td>
</tr></table>
答案 2 :(得分:0)
首先删除数组名称之间的空格 可以是employee_details 您可以这样做
<table>
<thead>
<tr>
<th> First Name </th>
<th> Lats Name </th>
<th> Age </th>
</tr>
</thead>
<tbody>
<tr *ngFor= let array of employee_details>
<td> {{array.first}} </td>
<td> {{array.last_name}} </td>
<td> {{array.age}} </td>
</tr>
</tbody>
</table>
希望它能回答您的问题