Skip to content

Commit 24f93b8

Browse files
linkedsignal: signalFromControl (#20)
Co-authored-by: Johannes Hoppe <[email protected]>
1 parent 0fc4aaf commit 24f93b8

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

blog/2024-11-linked-signal/README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Angular 19: Introducing LinkedSignal for Responsive Local State Manageme
33
author: Johannes Hoppe and Ferdinand Malcher
44
55
published: 2024-11-04
6-
lastModified: 2024-11-04
6+
lastModified: 2024-11-15
77
keywords:
88
- Angular
99
- JavaScript
@@ -294,6 +294,46 @@ To complete the flow, it would also be possible to modify the book data and send
294294
> We presented the Resource API in a separate blog post (in German 🇩🇪): **[Die neue Resource API von Angular](https://angular-buch.com/blog/2024-10-resource-api)**
295295
296296

297+
### Connecting Reactive Forms with Signals
298+
299+
We can even use Linked Signals for building helpers that connect other worlds to signals.
300+
For example, this wrapper function synchronises a `FormControl` (or any other control) from Angular's Reactive Forms with a signal.
301+
Data is synchronized bidirectionally: When the form value changes (`valueChanges`), the signal value will be updated.
302+
The signal returned from the function is writable, so whenever we change the value in the signal, the form value will be updated (`setValue()`).
303+
304+
```ts
305+
export function signalFromControl<T>(control: AbstractControl<T>) {
306+
const controlSignal = linkedSignal(
307+
toSignal(control.valueChanges, { initialValue: control.value })
308+
);
309+
effect(() => control.setValue(controlSignal()));
310+
return controlSignal;
311+
}
312+
```
313+
314+
In this example, you see an effect that establishes a **reactive listener**, which automatically responds to changes in signals. The function inside `effect()` makes sure that whenever the signal `controlSignal` changes, the form control value is updated via `setValue()`. This creates a **two-way synchronization** between the signal and the form control.
315+
For a more detailed exploration of `effect()` and its capabilities, read our article: **[Angular 19: Mastering effect and afterRenderEffect](/blog/2024-11-effect-afterrendereffect)**.
316+
317+
The helper can be used as follows:
318+
319+
```ts
320+
bookForm = new FormGroup({
321+
isbn: new FormControl('', { nonNullable: true }),
322+
title: new FormControl('', { nonNullable: true }),
323+
});
324+
325+
title = signalFromControl(this.bookForm.controls.title);
326+
327+
// ...
328+
// Form value will be updated to 'Angular'
329+
this.title.set('Angular');
330+
331+
// Signal value will be updated to 'Signals'
332+
this.bookForm.setValue({ isbn: '123', title: 'Signals' });
333+
```
334+
335+
336+
297337
## Linked Signal vs. Other Signals
298338

299339
Here’s a quick comparison with other types of signals in Angular:

0 commit comments

Comments
 (0)