【发布时间】:2021-01-17 17:27:55
【问题描述】:
我得到这个 api “http://dummy.restapiexample.com/api/v1/employees” 然后我尝试将它与 *ngFor 一起使用,但发生了这个问题 “ERROR 错误:找不到类型为‘object’的不同支持对象‘[object Object]’。NgFor 仅支持绑定到 Iterables,例如数组。”
app.component.ts
import { Component } from '@angular/core';
import { HttpClient } from "@angular/common/http";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
employes;
constructor(private http : HttpClient) {
this.http.get('http://dummy.restapiexample.com/api/v1/employees').subscribe(employ =>
{this.employes = employ})
}
}
app.component.html
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Salary</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let empo of employes ; let i = index '>
<th scope="row">{{i + 1}}</th>
<td>{{empo.employee_name}}</td>
<td>{{empo.employee_salary}}</td>
<td>{{empo.employee_age}}</td>
</tr>
</tbody>
</table>
【问题讨论】:
-
这里可能有一些问题; 1.您不会将“employes”作为数组启动,因此它将是“未定义”的。 2. 你是否验证了对 api 调用的响应实际上返回了一个 'employes' 数组?即'employ'实际上是否返回一个数组?
标签: angular