Last active
May 9, 2020 11:07
-
-
Save AvocadoVenom/5e9c1b20a6376aaf0c8cc25018ed9ffa to your computer and use it in GitHub Desktop.
Child-to-parent communication using EventEmitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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