import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` `, }) export class ChildComponent { @Output() clicked = new EventEmitter(); onClick() { this.clicked.emit('button clicked!'); } } @Component({ selector: 'app-parent', template: `{{ message }}
`, }) export class ParentComponent { message: string; handleClick(message) { this.message = message; } }
import { Injectable, EventEmitter } from '@angular/core'; @Injectable() export class NotificationService { messageAdded = new EventEmitterIn this example, we have a service that provides a way for components to add messages to a notification system. The service has an EventEmitter that emits a messageAdded event when a new message is added. Components can listen for this event to receive notifications. Package Library: @angular/core(); add(message: string) { this.messageAdded.emit(message); } }