Selectors for Derived State in Angular
📖 Scenario: You are building a simple Angular app to manage a list of products in a store. Each product has a name and a price. You want to show the total price of all products combined.
🎯 Goal: Create an Angular standalone component that uses a signal to hold the products list, a signal for a price threshold, and a derived signal (selector) to calculate the total price of products that cost more than the threshold.
📋 What You'll Learn
Create a signal called
products with the exact array of objects: [{ name: 'Pen', price: 5 }, { name: 'Notebook', price: 15 }, { name: 'Backpack', price: 50 }]Create a signal called
priceThreshold with the value 10Create a derived signal called
totalExpensive that sums the prices of products with price greater than priceThreshold()Create a standalone Angular component named
ProductTotalComponent that displays the total price from totalExpensive()💡 Why This Matters
🌍 Real World
Using selectors (derived signals) helps keep your UI in sync with data changes automatically, which is common in modern Angular apps.
💼 Career
Understanding signals and derived state is important for Angular developers to build efficient, reactive user interfaces.
Progress0 / 4 steps