Angular Signals
Gamechanger fΓΌr reaktive Entwicklung
Yannick Baron
@yannick_baron
Slide 2
Slide 2 text
Angular application
Components are the main building blocks
Consist of a TypeScript class, an HTML template, and CSS styles
Essentially define your custom DOM nodes
Component templates can use more components
π An application is essentially a tree of components
Increase Counter
Reset
π note: setting a variable on the component updates the template
Slide 4
Slide 4 text
The Magic of Angular: Zone.js
Zones are execution contexts that track asynchronous operations
Achieved via monkey patching
Angular creates a root zone for the application ("NgZone")
When an event or asynchronous task occurs, Zone.js notifies Angular
Angular triggers change detection and updates the views
Slide 5
Slide 5 text
Angular Change detection
Start from root component
Walk through the component tree
Check template bindings
If change detected, update DOM node
Changes propagated to children
Slide 6
Slide 6 text
Downsides of Zone.js approach
Reliant on Zone.js monkey patching
Runs more often than necessary
Sometimes leads to unpredictable behavior
Recheck all template bindings
π Advanced concepts to learn to improve performance:
OnPush, Immutability, runOutsideAngular, ...
Slide 7
Slide 7 text
Reactive Angular with RxJS
Use OnPush strategy
Reactive state management
Subscribe via Async pipe
Async pipe notifies the change detector
π Only check relevant components
Slide 8
Slide 8 text
Downsides of RxJS (for template rendering)
Observables asynchronous by default
AsyncPipe default null value
Subscription management
Recheck all template bindings
No reactive inputs
Slide 9
Slide 9 text
Enter Signals
Reactive primitive
Hold value, notify when value changes
Integrated with Angular
π Results in knowing exactly which nodes need updating
Slide 10
Slide 10 text
Same component as before
@Component()
export class CounterComponent {
counter = 0;
increment() {
this.counter++;
}
reset() {
this.counter = 0;
}
}
{{ counter }}
Increase Counter
Reset
Slide 11
Slide 11 text
Same component as before with signals
@Component()
export class CounterComponent {
counter = signal(0);
increment() {
this.counter.update((count) => count + 1);
}
reset() {
this.counter.set(0);
}
}
{{ counter() }}
Increase Counter
Reset
Slide 12
Slide 12 text
Signals API
signal()
Create a writable Signal
set()
Set a new value to the Signal
update()
Update the Signal's value based on its current one
computed()
Create a read only Signal deriving its value from other Signals
Slide 13
Slide 13 text
Stop ... demo time
Slide 14
Slide 14 text
Signals API - effect()
React to signal value changes
Used for side effects
logging, persisting to browser storage, ...
Can not write to Signals by default
Reliant on the injection context
Slide 15
Slide 15 text
RxJS is here to stay
RxJS is used to handle asynchronicity
Plenty of useful operators
error handling, retrying, scheduling, buffering, ...
RxJS interop provided by angular
toObservable / toSignal / takeUntilDestoyed
Used for asynchronous side effects
@ngrx/effects, ComponentStore effects
Slide 16
Slide 16 text
What the future holds
Zone-less applications
Signal based components
input, output, model
Signal based, synchronous APIs
π Less concepts to learn
π Better performance out of the box
π Less wrestling with the CD
Slide 17
Slide 17 text
Read more about upcoming changes
π https://github.com/angular/angular/discussions
π https://angular.io/guide/signals