Component Files
The component is located atsrc/app/pages/hero/:
hero-page.component.ts- Component logichero-page.component.html- Template
TypeScript Implementation
Signal Properties
The component maintains two reactive signals:name
age
Both signals are initialized with default values representing Ironman. The
resetForm() method returns to these original values.Methods
getHeroDescription()
Returns a formatted string combining the hero’s name and age:changeHero()
Switches the hero to Spiderman with corresponding age:changeAge()
Updates only the age to 60:resetForm()
Resets both signals to their initial values:Template Features
Signal Reading
Signals are read by invoking them as functions:Signal Transformation
You can call methods on signal values directly in templates:toUpperCase() method on the string returned by name().
Method Calls
Component methods can be called in templates:Event Binding
Buttons trigger methods using Angular’s event binding:Data Flow
- Initial State: Component loads with Ironman (age 45)
- User Interaction: User clicks a button
- Signal Update: Method updates signal(s) using
.set() - Template Update: Angular automatically re-renders affected template sections
- Computed Values: Any methods using signals are re-executed
The template uses a definition list (
<dl>, <dt>, <dd>) to display hero properties in a structured format.Key Patterns
Using .set() for Direct Assignment
All updates use theset() method for direct value assignment:
Reading Signals in Methods
When reading signals in component methods, invoke them as functions:Multiple Signal Updates
You can update multiple signals in a single method:Key Takeaways
- Simple state management - Signals provide reactive state without complex setup
- Computed values - Methods that read signals automatically react to changes
- Direct transformations - Signal values can be transformed directly in templates
- Multiple updates - Multiple signals can be updated in a single method
- Type safety - TypeScript infers signal types from initial values