Shape on Action Pattern

PHOTO EMBED

Sat Dec 17 2022 03:51:27 GMT+0000 (Coordinated Universal Time)

Saved by @ilivanilton #angular

// app.component.html
<div *ngIf="selectedProduct$ | async as product">
  	{{ product.name }}
  </button>
</div>


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


// product.service.ts
products$ = this.categorySelectedAction$.pipe(
  switchMap( catId => this.http.get<Product[]>(this.url+'?cat='+catId))
  .pipe(...);
        
private productSelectedSubject = new Subject<number>();
productSelectedAction$ = this.productSelectedSubject.asObservable();

selectedProduct$ = combineLatest([
  this.products$,
  this.productSelectedAction
]).pipe(
  map(([products, selectedProductId]) => 
   products.find(product => product.id === selectedProductId)
  ));

content_copyCOPY