Home >Web Front-end >JS Tutorial >Angular - Introduction to linkedSignal
Angular 19 is on the horizon, and it’s bringing a host of exciting new features to the table. One of the most notable additions is the linkedSignal primitive, which promises to revolutionize the way we handle reactive programming in Angular applications.
Traditionally, implementing reset patterns in Angular involved using computed() signals. While effective, this approach has limitations. When you need to set the value of a signal explicitly, it becomes a read-only signal, hindering flexibility.
linkedSignal addresses this limitation by providing a writable signal that automatically updates its value based on changes to a source signal. This enables us to create a seamless synchronization between the two, ensuring a glitch-free user experience.
While linkedSignal will have multiple overloads, two of them are worth mentioning:
This overload allows you to create a linkedSignal that computes its value based on the value of a source signal. Here's an example:
import { signal, linkedSignal } from '@angular/core'; const sourceSignal = signal(0); const linkedSignal = linkedSignal({ source: this.sourceSignal, computation: () => this.sourceSignal() * 5, });
In this example, linkedSignal will always be twice the value of sourceSignal. Whenever sourceSignal changes, linkedSignal will automatically recompute its value. Here’s a more real-world example of linkedSignal:
The CourseDetailComponent component accepts a courseId as input and displays the number of enrolled students. We aim to reset the student count whenever the selected courseId changes. This necessitates a mechanism to synchronize two signals: the courseId and the studentCount.
While the usage of computed() can be effective in deriving values from other signals, they are read-only. To dynamically update the studentCount based on changes in the courseId, we leverage the linkedSignal primitive. By creating a writable signal linked to the courseId, we can both set the studentCount explicitly and automatically update it whenever the courseId changes. This approach provides a robust and flexible solution for managing signal dependencies and ensuring data consistency.
For simpler scenarios, you can use a shorthand syntax to create linkedSignal:
const sourceSignal = signal(10); const linkedSignal = linkedSignal(() => sourceSignal() * 2);
This shorthand syntax is equivalent to the first overload, but it’s more concise and easier to read.
linkedSignal is a powerful new tool in Angular's reactive toolkit. By understanding its core concepts and usage patterns, you can create more robust, responsive, and user-friendly Angular applications. With its ability to combine the best aspects of computed() and writable signals, linkedSignal is poised to become an indispensable tool for Angular developers. You can learn more about linkedSignals from this stackblitz.
The above is the detailed content of Angular - Introduction to linkedSignal. For more information, please follow other related articles on the PHP Chinese website!