With Book class defined as:
class Book {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
I go ahead and create Observable object books$:
const book1: Book = new Book(1, "Title 1");
const book2: Book = new Book(2, "Title 2");
function booklist(observer) {
observer.next(book1);
observer.next(book2);
observer.complete();
return {unsubscribe() {}};
}
this.books$ = new Observable(booklist);
Checking it with:
this.books$.subscribe(each => console.log(each));
prints:
Book {id: 1, name: "Title 1"}
Book {id: 2, name: "Title 2"}
But using this observable with ngFor in the html template:
<ul>
<li *ngFor="let book of books$ | async" >
Id: {{book.id}}, Name: {{book.name}}
</li>
</ul>
raises the ERROR:
Error: Cannot find a differ supporting object '[object Object]' of type 'Title 2'. NgFor only supports binding to Iterables such as Arrays.
How to solve this problem? Here is the link to Stackblitz project:
https://stackblitz.com/edit/angular-ivy-isebs7?file=src%2Fapp%2Fapp.component.ts