I am writing a e-commerce application using Angular 17.
I have a list of products that I iterate through using the @for block from angular/core in one of my HTML component files.
Here is an oversimplified version of my code:
@for(product of products; track product){
<h3>{{product.name}}</h3>
<span>{{product.price}}</span>
}
My issue is that the products list might change because of user input (filtering through products).
How can I change the HTML for my @for in order to be mindful of list changes and sync automatically without the need to refresh my page?
So far I have only found information about using ngFor, but I really want to use the new @for block.
We can use
fromEventand a method where we store the original value and a rxjs stream, where we listen for the eventinputusingfromEventand then we usemapto modify the values usingfilterarray method, finally we return the values.fromEventdoes not get fired initially, so we usemergeandofto fire the initial value which is the entire array!Best resource for learning about RXJS operators used here!
Stackblitz Demo