Retrieve on Action Pattern

PHOTO EMBED

Sat Dec 17 2022 03:32:48 GMT+0000 (Coordinated Universal Time)

Saved by @ilivanilton #angular

// app.component.html
<div *ngIf="products$ | async as products">
  <button *ngFor='let product of products'>
  	{{ product.name }}
  </button>
</div>


// app.component.ts
products$ = this.productService.products$;


// product.service.ts
private categorySubject = new Subject<number>();
categorySelectedAction$ = this.categorySubject.asObservable();

products$ = this.categorySelectedAction$.pipe(
  switchMap( catId => this.http.get<Product[]>(this.url+'?cat='+catId))
  .pipe(
  	tap(console.log),
  	catchError(this.handleError)
   )
);

selectedCategoryChanged(categoryId: number): void{
  this.categorySUbject.next(categoryId);
}
content_copyCOPY

https://www.youtube.com/watch?v=uv_sblwIJag&t=232s