angular - 尝试区分“[object Object]”时出错。角度中只允许使用数组和可迭代对象
问题描述
我正在尝试在前端显示数据,但出现上述错误。如何解决这个问题?
服务.ts
rec_source(){
var headers = new HttpHeaders().set('Authorization', 'Token ' + localStorage.getItem('usertoken'));
headers.append('Accept', 'application/json');
var options = {
headers: headers
};
return this.httpClient.get('api/recruit/fetch_recruitment_details',options)
}
组件.ts
sources:any = [];
constructor(private Authentication:AuthService) { }
ngOnInit() {
this.Authentication.rec_source()
.subscribe(source => {
this.sources = source;
});
}
组件.html
<table *ngIf="sources" class="row-border hover">
<thead>
<tr>
<th>Data</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let source of sources">
<td>{{ source}}</td>
</tr>
</tbody>
</table>
//需要显示的数据
解决方案
您的 api 响应不是数组或可迭代的,即错误所说的。它是对象,不能用 迭代*ngFor
。也许您想迭代data_candidate_source
或data_new_hiredate
. 在这种情况下,您需要为source
组件中的属性分配正确的属性:
this.sources = source.data_candidate_source
或者当您想遍历对象时,您可以使用新添加的键值管道。
推荐阅读
- python - 无法更改 numpy 数组中的值
- angular - 角度路由错误:“ViewDestroyedError:尝试使用已破坏的视图:detectChanges”
- javascript - 动态调用变量
- r - 如何在 R 中创建具有比例和数值的热图
- python - 对变量的赋值正在更改对该变量先前值的所有其他引用
- mysql - 如何从 PhpMyAdmin 取消复制表
- python - 破碎的 dag 试图将气流连接到雪花
- excel - 想要使用 VBA、Excel 和 Selenium 抓取从页面返回的文本
- flutter - 在 Flutter 中显示来自 Internet 的图像
- python - 如何在 django 模板中将秒转换为 hh:mm:ss