Signals - Properties VS Signals
What are Signals?
Signals are wrappers around values.
- Signals are not very old in Angular.
- They were introduced around 1–1.5 years ago.
Normal Property vs Signal (Basic Idea)
Normal Property Example
data = 10;
- UI updates when value changes.
- But TypeScript file cannot detect when the value changes.
Signal Example
count = signal(0);
- Signal is created using the
signal()function. signal()works like a function around a value.- Signal sends a notification when its value changes.
- Changes can be detected inside the TypeScript file.
Displaying Normal Property in UI
<h2>{{data}}</h2>
- Normal interpolation is used.
- Value is displayed correctly.
Displaying Signal in UI
<h2>Signal : {{count()}}</h2>
- Signal is accessed using parentheses
(). - Double curly braces are still used.
Updating Normal Property
HTML
<button (click)="updateData()">Update Data</button>
TypeScript
updateData(){
this.data++
}
- Value increases.
- UI updates.
- No way to detect this change inside TypeScript automatically.
Updating Signal
HTML
<button (click)="updateCount()">Update Count</button>
TypeScript
updateCount(){
this.count.set(this.count()+1)
}
- Signal value is updated using
.set() - Value increases correctly.
- UI updates automatically.
Effect in Signals
What is Effect?
effectis a feature from Angular core.- It runs when a signal value changes.
- Used to detect signal changes inside TypeScript.
- Mostly used inside the constructor.
Effect Example from Code
constructor(){
effect(()=>{
console.log("this is count", this.count())
if(this.count() == 10){
this.count.set(0)
}
})
}
What Happens Here?
- Every time
countchanges, effect runs. - Latest value of
countis logged. - When
countreaches10, it resets to0.
Important Difference Observed
Normal Property inside Effect
// console.log("this is data", this.data);
- Normal property does NOT trigger effect on update.
- No notification is received when
datachanges.
Signal inside Effect
console.log("this is count", this.count())
- Signal triggers effect every time value changes.
- Change is detected inside TypeScript.
Why Signals are Reactive?
- Signals react when their value changes.
- Effect listens to signals.
- Any condition or action can be performed immediately.
- Example actions:
- API calls
- Reset value
- Redirect
- Login / Logout
Counter Example with Signal
- Initial value starts from
0 - Button increments value
- When value reaches
10, it resets to0 - All logic handled reactively using
effect
Types of Signals
There are three types of Signals:
- Writable Signals
- Value can be changed
- Example:
signal(0) - Computed Signals
- Read-only
- Derived from other signals
- Driven signals
- Effect Signals
- Run inside
effect - Execute only when signal changes
Change Detection Difference
- Signals update only the exact place where they are used.
- Normal properties can affect the whole component.
- Signals do not re-render the whole component.
Signals vs React State
- React state re-renders the whole component.
- Signals update only required parts.
- Signals are faster than React state.
- Signals never re-render the entire component.
Syntax Difference
Normal Property
this.data = 20;
Signal
this.count.set(20);
- Signals use
set()orupdate() - Normal properties do not use functions