Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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 10
Create 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
1
Set up the products signal
Create a signal called products with this exact array of objects: [{ name: 'Pen', price: 5 }, { name: 'Notebook', price: 15 }, { name: 'Backpack', price: 50 }]
Angular
Hint
Use signal from Angular to create a reactive variable holding the products array.
2
Add the price threshold signal
Add a signal called priceThreshold and set it to the number 10
Angular
Hint
Use signal to create a number variable holding the threshold value.
3
Create the derived signal for total price
Create a derived signal called totalExpensive that calculates the sum of prices of products where the product's price is greater than priceThreshold(). Use products() and priceThreshold() inside the derived signal.
Angular
Hint
Use a signal with a function that filters and sums prices based on the threshold.
4
Display the total price in the template
Update the component's template to display the text Total Price: followed by the value of totalExpensive(). Use Angular's interpolation syntax {{ }}.
Angular
Hint
Use Angular interpolation {{ totalExpensive() }} inside the template string.
Practice
(1/5)
1. What is the main purpose of using createSelector in Angular state management?
easy
A. To dispatch actions to the store
B. To directly modify the state values
C. To compute derived data from the state efficiently
D. To subscribe to HTTP requests
Solution
Step 1: Understand what createSelector does
createSelector is used to create selectors that compute derived data from the state without modifying it.
Step 2: Differentiate from other store operations
Modifying state or dispatching actions are done by reducers and actions, not selectors.
Final Answer:
To compute derived data from the state efficiently -> Option C
Quick Check:
Derived data = createSelector [OK]
Hint: Remember: selectors read and compute, not modify state [OK]
Common Mistakes:
Confusing selectors with actions or reducers
Thinking selectors modify state
Assuming selectors handle side effects
2. Which of the following is the correct syntax to create a selector that derives the total count from a list in the state using createSelector?
easy
A. const selectTotal = createSelector(list => list.length, selectList);
B. const selectTotal = createSelector(selectList, list => list.length);
C. const selectTotal = createSelector(selectList, list => list.count);
D. const selectTotal = createSelector(selectList, list => list.size());
Solution
Step 1: Check the order of arguments in createSelector
The first argument(s) are input selectors, followed by a projector function.
Step 2: Verify the projector function logic
The projector function receives the selected data and returns the derived value. Using list.length correctly gets the count.
Final Answer:
const selectTotal = createSelector(selectList, list => list.length); -> Option B
Quick Check:
Input selectors first, then projector function [OK]
Hint: Input selectors first, projector function last in createSelector [OK]
Common Mistakes:
Swapping input selectors and projector function order