Created
January 23, 2023 10:49
-
-
Save thovo/d252250af8c64e5177db070c2eee1d2c to your computer and use it in GitHub Desktop.
How parent component pass information to child component in Angular
This file contains 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, EventEmitter, Input, OnInit, Output } from '@angular/core'; | |
@Component({ | |
selector: 'app-child', | |
templateUrl: './child.component.html', | |
styleUrls: ['./child.component.scss'] | |
}) | |
export class ChildComponent implements OnInit { | |
// The basic need of a child | |
@Input() food: string; | |
@Input() drink: string; | |
@Input() game: string; | |
// He/she needs some attention | |
@Output() crying: EventEmitter<boolean> = new EventEmitter(false); | |
constructor() { } | |
ngOnInit(): void { | |
} | |
} |
This file contains 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, Input, OnInit } from '@angular/core'; | |
@Component({ | |
selector: 'app-child', | |
templateUrl: './child.component.html', | |
styleUrls: ['./child.component.scss'] | |
}) | |
export class ChildComponent implements OnInit { | |
// The basic need of a child | |
@Input() food: string; | |
@Input() drink: string; | |
@Input() game: string; | |
constructor() { } | |
ngOnInit(): void { | |
} | |
} |
This file contains 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
<!-- Inside parent component, or we can think, parent's house --> | |
<div class="parent"> | |
<div class="child"> | |
<app-child [drink]="'milk'" [food]="'cookie'" [game]="'chess'"></app-child> | |
</div> | |
</div> |
This file contains 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
<!-- Inside parent component, or we can think, parent's house --> | |
<div class="parent"> | |
<div class="child"> | |
<app-child [drink]="'milk'" [food]="'cookie'" [game]="'chess'" (crying)="hug()"></app-child> | |
</div> | |
</div> |
This file contains 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
<!-- Inside parent component, or we can think, parent's house --> | |
<div class="parent"> | |
<div class="child"> | |
<app-child></app-child> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment