尝试区分“[object Object]”时出错。 angular 中只允许数组和可迭代对象
Error trying to diff '[object Object]'. Only arrays and iterables are allowed in angular
我试图在前端显示数据,但出现上述错误。如何解决?
service.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)
}
component.ts
sources:any = [];
constructor(private Authentication:AuthService) { }
ngOnInit() {
this.Authentication.rec_source()
.subscribe(source => {
this.sources = source;
});
}
component.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
或者当你想遍历对象时,你可以使用新添加的 keyvalue pipe。
我试图在前端显示数据,但出现上述错误。如何解决?
service.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)
}
component.ts
sources:any = [];
constructor(private Authentication:AuthService) { }
ngOnInit() {
this.Authentication.rec_source()
.subscribe(source => {
this.sources = source;
});
}
component.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
或者当你想遍历对象时,你可以使用新添加的 keyvalue pipe。