问题
I'm trying to display the data on the front end but i get the above error. How to resolve this?
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>
//the data that needs to be displayed
//the error
回答1:
Your api response is not an array or iterable, that what the error says. It is object, which is not iterable with *ngFor
. Maybe you wanted to iterate data_candidate_source
or data_new_hiredate
. In such case you need to assign correct property to your source
property in component:
this.sources = source.data_candidate_source
Or when you want to iterate through object then you can use newly added keyvalue pipe.
来源:https://stackoverflow.com/questions/53044045/error-trying-to-diff-object-object-only-arrays-and-iterables-are-allowed-in