I have searched and found some information here: Iterate array inside object Angular 6 but I cannot get it to work properly.
I have an API call and it returns an array of objects (DocumentHead):
Each of these has an object array called documents:
Each DocumentHead will always have an entry for documents so code below works (sort of)
<li *ngFor="let docHead of DocumentHead;">
{{docHead.documents[0].id}}
</li>
(Code {{docHead.documents.id}} says it is undefined)
DocumentHead.documents[0].id will return at least 1 entry id for each of the 5 entries. What I want is that for each DocumentHead, it will list the id of each entry in documents I currently have this:
<li *ngFor="let docHead of DocumentHead;">
<div *ngFor="let docs of docHead.documents[i]; let i = index; ">
<h3> {{docs.category.name}} </h3>
</div>
</li>
but getting error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
(It won’t allow “let docs of docHead.documents, gives error: Type DocumentsModel is not assignable to type (DocumentsModel & NgIterable) | undefined | null )
Tried code from Loop through array of JSON object with *ngFor (Angular 4)
<div *ngFor="let recipient of docHead.documents | keyvalue">
{{recipient.key}} --> {{recipient.value}}
</div>
And getting this output
This is close to what I want. (In html component getting error: Argument type DocumentsModel is not assignable to parameter type {[p: string]: number | string | boolean …>)
How can I iterate over docHead.documents so that my list shows the id of all documents?
ETA Model and call
DocumentHeadModel:
export class DocumentHeadModel {
id: string;
documents: DocumentsModel;
}
export class DocumentsModel {
id: string;
}
call:
getDocuments(id: any): Observable<DocumentHeadModel[]> {
return this.http.get<DocumentHeadModel[]>(this.myURL?id=` + id);
}