Last active
May 9, 2020 11:04
-
-
Save AvocadoVenom/f8d991a41cfdeecbb219b410cce9a6c4 to your computer and use it in GitHub Desktop.
Several ways to make an angular child-to-parent communication
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-child', | |
| template: `<h1>I am the child</h1>` | |
| }) | |
| export class ChildComponent { | |
| message = 'Hey there from the child!'; | |
| constructor() { } | |
| } |
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, ViewChild, AfterViewInit } from '@angular/core'; | |
| import { ChildComponent } from "./child.component"; | |
| @Component({ | |
| selector: 'app-parent', | |
| template: ` | |
| Message: {{ message }} | |
| <app-child></app-child> | |
| ` | |
| }) | |
| export class ParentComponent implements AfterViewInit { | |
| @ViewChild(ChildComponent) child; | |
| message:string; | |
| constructor() { } | |
| ngAfterViewInit() { | |
| this.message = this.child.message; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment