In a few Angular projects I have the same problem, whenever I try to call a function inside my HTML (that retrieves some value from the api) it triggers an infinite loop. In the example below it's getUser() that triggers the loop.
HTML
<ul>
<li *ngFor="let order of orders">
{{ getUser(order.orderNr).emailAddress }}
</li>
</ul>
component
private getOrdersList() {
this.orderService.getAll().subscribe(
orders => {
this.orders = orders;
}
);
}
public getUser(orderNr: number) {
return this.orderService.getUser(orderNr);
}
service
public getAll(): Observable<Order[]> {
return this.api.get<Order[]>('orders');
}
public getUser(orderNr: number) {
return this.api.get<void>('orders/'+orderNr);
}
I think it has something to do with the way Angular handles data but I'm fairly new to Angular and unsure how to retrieve this data without causing the loop. Perhaps someone more experienced can provide some help?