Angular Signals allow components to communicate with each other in a straightforward and efficient way. They provide a way for components to send messages to each other without having to directly reference each other’s properties or methods. This makes it easy to create modular, reusable components that can be easily connected and disconnected.
In this tutorial, we will cover everything you need to know about Angular Signals, including how to create them, how to send messages, and how to listen for messages. By the end of this guide, you will have a solid understanding of how to use Angular Signals to improve the communication between your components.
Creating a Signal
To create a Signal in Angular, you first need to import the Signal class from the "@angular/core" module. Then, you can create a new instance of the Signal class and assign it to a property in your component. Here’s an example of how you can create a Signal in Angular:
import { Signal } from "@angular/core";
export class MyComponent {
mySignal = new Signal();
}
Sending a Message
Once you have created a Signal in your component, you can use the .emit() method to send a message to any listeners that are subscribed to the Signal. The .emit() method takes one parameter, which is the message that you want to send. Here’s an example of how you can send a message using a Signal:
this.mySignal.emit("Hello, World!");
Listening for Messages
To listen for messages sent via a Signal, you need to subscribe to the Signal using the .subscribe() method. The .subscribe() method takes a callback function as a parameter, which will be called whenever a message is emitted on the Signal. Here’s an example of how you can listen for messages using a Signal:
this.mySignal.subscribe((message) => {
console.log(message);
});
Unsubscribing from a Signal
When you subscribe to a Signal, Angular will automatically unsubscribe when the component is destroyed. However, if you need to manually unsubscribe from a Signal, you can use the .unsubscribe() method. Here’s an example of how you can manually unsubscribe from a Signal:
const subscription = this.mySignal.subscribe((message) => {
console.log(message);
});
// Later, when you want to unsubscribe
subscription.unsubscribe();
Conclusion
Angular Signals are a powerful tool for facilitating communication between components in Angular applications. By creating Signals, sending messages, and listening for messages, you can build flexible and modular components that can easily communicate with each other. I hope this guide has been helpful in understanding how to use Angular Signals effectively in your projects. Happy coding!
awesome!!!! Thanks for detailed explanation, Please make a complete tutorial series on angular 18 with all new concepts.
Liked and subscribed! Thank you for the explanation
Really good explanation. Thank you
I appreciate the time and effort you put into each video.
Precise explanation, Thank you for your effort
Detailed explanation of signals with a real time example. Thanks for sharing the knowledge !!
can u share your github repo for the video example code?
Great video. You cleared all my doubts . Thanks A lot !!!
bro i need parent child concepts using output input
Amazing, complete video, Thanks!
Hats off to you🎉, the simplest tutorial of signal
This is basically Angular implementing React. Now we have fewer reasons to use React, lmao.
Very very useful and understandable example. Thanks you.
Thanks a lot
learned with pratical examples
Okay, so looks like Angular team want's to stop using ReactiveX(RxJS) – and use their own approach istead.
Looks like main idea of signals – remove third party RxJS from Angular, am I right ?
I am still not clear how we know that effect will not trigger infinite changes?
I am still confused on when to use signals? If i take a normal variable instead of a writable signal, then also i get the same result so what's the real use case of signal. Does normal variables uses Zone.js whereas signals are zone less?
I think mutate has been removed from angular 18, we can use something like this this.colors.update((value) => […value, 'yellow']);
Great video, thank you sir