Component Files
The component is located atsrc/app/pages/counter/:
counter-page.ts- Component logiccounter-page.html- Template
TypeScript Implementation
Signal Usage
This component demonstrates two approaches to state management:Traditional Property
Signal-based State
With
OnPush change detection, the traditional counter property won’t update in the template unless an event is triggered. The counterSignal works correctly because signals are reactive.Methods
incremento(value: number)
Increments or decrements both counters by the specified value:value- Amount to increment (positive) or decrement (negative)
- Uses
update()method with a callback function - Receives current value and returns new value
- Ensures immutable state updates
resetContador()
Resets both counters to zero:- Uses
set()method to directly assign a new value - Simpler than
update()when you don’t need the current value
Change Detection Strategy
OnPush change detection, which only checks for changes when:
- An input property reference changes
- An event is triggered in the component
- A signal used in the template changes
- Change detection is manually triggered
Inline Styles
The component includes inline styles for the buttons:Template Features
Signal Invocation
Signals are invoked as functions in templates:Event Binding
Click events are bound using Angular’s event binding syntax:Key Takeaways
- Signals are reactive - They automatically trigger template updates
- OnPush optimization - Use signals with OnPush for better performance
- Two update methods - Use
set()for direct values,update()for computed values - Function invocation - Signals must be called as functions in templates:
counterSignal()