Skip to content

Instantly share code, notes, and snippets.

@thovo
Created January 23, 2023 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thovo/d252250af8c64e5177db070c2eee1d2c to your computer and use it in GitHub Desktop.
Save thovo/d252250af8c64e5177db070c2eee1d2c to your computer and use it in GitHub Desktop.
How parent component pass information to child component in Angular
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 {
}
}
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 {
}
}
<!-- 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>
<!-- 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>
<!-- 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