There's a list of products coming from an API. The products are paginated and the user can switch to another page. The simplified template looks like this:
<ul>
<li *ngFor="let product of products$ | async">{{ product.name }}</li>
</ul>
<button type="button" (click)="gotoPage(1)">1</button>
<button type="button" (click)="gotoPage(2)">2</button>
The component does look like this:
export class ProductsComponent implements OnInit {
products$: Observable<Product[]>;
constructor(
private service: ProductService
) { }
ngOnInit() {
this.products$ = this.service.getAll({page: 1});
}
gotoPage(page: number): void {
this.products$ = this.service.getAll({page: page});
}
}
My questions are: Is this the correct way to update the Obersavble? Or does this produce memory leaks?
As a note: The URL will not change and the component should not reloaded on pagination change.