Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
Last active May 9, 2020 11:07
Show Gist options
  • Select an option

  • Save AvocadoVenom/5e9c1b20a6376aaf0c8cc25018ed9ffa to your computer and use it in GitHub Desktop.

Select an option

Save AvocadoVenom/5e9c1b20a6376aaf0c8cc25018ed9ffa to your computer and use it in GitHub Desktop.
Child-to-parent communication using EventEmitter
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<button (click)="sendMessage()">Send Message</button>
`
})
export class ChildComponent {
message: string = "Hey there from the child!";
@Output() fromChild = new EventEmitter<string>();
constructor() { }
sendMessage() {
this.fromChild.emit(this.message);
}
}
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
Message: {{message}}
<app-child (fromChild)="setMessage($event)"></app-child>
`
})
export class ParentComponent {
message:string;
constructor() { }
setMessage($event) {
this.message = $event;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment