emitter in angular
- User
emitter in angular
- Assistant
In Angular, an emitter is a class that is used to create and emit custom events. It can be used to communicate between parent and child components, or even between unrelated components in your application.
To use an emitter, you first need to import the EventEmitter class from the @angular/core library. Then you can create an instance of this class and call its emit() method to send out an event.
Here's an example of how to use an emitter in Angular:
Child component:
import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="sendMessage()">Send Message</button> ` }) export class ChildComponent { @Output() messageEvent = new EventEmitter<string>(); sendMessage() { this.messageEvent.emit('Hello from the child!'); } }