Code: Select all
filteredProducts$!: Observable
;
selectedCategory$: Observable;
private store = inject(Store);
constructor() {
this.selectedCategory$ = this.store.select(
CategorySelectors.selectSelectedCategory
);
this.filteredProducts$ = combineLatest([
this.store.select(ProductSelectors.selectAllProducts),
this.store.select(CategorySelectors.selectSelectedCategoryId),
]).pipe(
map(([products, selectedCategoryId]) => {
if (selectedCategoryId) {
return products.filter((p) => p.categoryId === selectedCategoryId);
}
return products;
})
);
}
ngOnInit() {
this.store
.select(ProductSelectors.selectAllProducts)
.subscribe((products) => {
if (!products || products.length === 0) {
this.store.dispatch(ProductActions.loadProducts());
}
});
}
< /code>
html < /p>