🛒 Signal Example 13: My Cart (Add/Remove Product)

🛍️ Discover how Angular Signals can power a dynamic shopping cart! This example demonstrates real-time add/remove actions and a live total, all managed reactively.

  • Product A$10
  • Product B$15
  • Product C$20
Total Price:$0

🛒 Deep Dive: Building a Reactive Shopping Cart with Angular Signals

Shopping carts are a classic example of dynamic, interactive state in web apps. With Angular signals, you can build a cart that updates instantly as users add or remove products—no manual refresh, no complex state management, just pure reactivity.

Signals let you manage cart state, compute totals, and update the UI in real time, all with minimal code and maximum clarity.

How This Example Works

  • Signal Declarations: The product list and cart are both signals: products = signal([...]), cart = signal([]).
  • Adding/Removing: Clicking Add or Remove updates the cart signal, which instantly updates the UI.
  • Computed Values:totalPrice and selectProductsIds are computed signals that recalculate automatically as the cart changes.
  • Reactive UI: The template uses @for and @if for a fully reactive, declarative cart experience.
  • No Subscriptions Needed: Signals handle all reactivity for you. The UI and state stay in sync in real time.

Why Use Signals for Carts?

  • Instant Feedback: Users see their cart update as they interact, with no lag or manual refresh.
  • Performance: Only the parts of the UI that use the signal are updated.
  • Readability: The code is easy to follow and maintain, even as features grow.
  • Fine-Grained Reactivity: Signals track exactly where they’re used, so updates are efficient and instant.
  • Real-World Use: This pattern works for any cart, wishlist, or dynamic list in your app.
Angular signals make building interactive, real-world features like shopping carts simple, efficient, and a joy to use!