In this exercise, you will practice using NGRX signal store to create statefull signal based services in angular.
Create an Angular Signal Store to manage the state of two numbers (x
and y
). Practice setting up state, using computed values, and creating methods for modifying state.
- Create a new Signal Store service using
@ngrx/signals
. - Define two state properties:
x
andy
with initial values of0
.
- Add a computed property
sum
that calculates the sum ofx
andy
.
- Add methods to the store for updating
x
andy
independently:updateX(value: number)
: Sets the state ofx
.updateY(value: number)
: Sets the state ofy
.
- Create a component that displays
x
,y
, andsum
. - Provide the store in the component level.
- Add input fields or buttons to allow users to modify
x
andy
through the store methods.
- Use Angular's dependency injection to provide the store to the component.
- Display the computed
sum
in real-time as the state updates.
In this exercise you learned how to:
- Set up a Signal Store to manage state.
- Use computed properties to calculate values based on the state.
- Create methods to modify state and see the effect on computed properties.