child parent component communication code example
Example 1: angular @Output()
/* @Output()
- allows data to flow from the child class to the parent class
- normally initialized with EventEmitter
*/
// Parent Component
import { Component } from '@angular/core'
@Component({
selector: app-component,
template:
`
<app-item-output (newItemEvent)='addItem($event)'> </app-item-output>
<ul>
<li *ngFor='let item of items'>{{item}}</li>
</ul>
`
/* Template Notes
- (newItemEvent)='addItem($event)' -- the event binding
tells Angular to connect the event in the child
component, newEventItem, to the method in the parent,
addItem(), and that the event that the child is
notifying the parent of is to be the argument of the
method.
- $event contains the data that the child component is
passing to the parent.
*/
})
export class AppComponent {
items = ['item1', 'item2', 'item3'];
addItem(newItem: string){
this.items.push(newItem);
}
/* Class Notes
- `[item]` -- @Input() target from child
- `currentItem` -- property from parent
- `(deleteRequest)` -- the target @Output event from
child
- `crossOffItem($event)` -- method from parent;
*/
}
// Child Component
import { Output, EventEmitter } from '@angular/core';
@Component({
selector: app-item-output,
template:
`
<label>Add an item: <input #newItem></label>
<button (click)='addNewItem(newItem.value)'>Add to parent's list</button>
`
/* Template Notes
- #newItem stores whatever is typed into the input
- (click) event is bound to addNewItem method and
passes newItem.value to the component class
*/
})
export class ItemOutputComponent {
@Output() newItemEvent = new EventEmitter<string>();
addNewItem(value: string) {
// this method emits the value of newItemEvent
this.newItemEvent.emit(value);
}
/* Component Class Notes
- @Output marks the property as a way for data to go
from the child component to the parent component
- newItemEvent is the name of the @Output
- new EventEmitter<string>() -- tells Angular to create
a new event emitter of type string
- addNewItem(value: string) -- when the user clicks the
button the child component lets the parent component
know about the event and gives the data to the parent.
*/
}
Example 2: angular send data to parent component
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<button (click)="sendMessage()">Send Message</button>
`,
styleUrls: ['./child.component.css']
})
export class ChildComponent {
message: string = "Hola Mundo!"
@Output() messageEvent = new EventEmitter<string>();
constructor() { }
sendMessage() {
this.messageEvent.emit(this.message)
}
}
Example 3: how to pass variable via component after it's updated angular
// you can try like this, here you want to get the data from the parent component to child component, Here your parent component is ParentComponent and child component is app-child so here for getting the data from parent to child we can use ngOnChanges(changes: SimpleChanges)
// ParentComponent.html
<app-child
[data]="data"
(filterEmmiter)="filter($event)">
</app-child>
// child.component.ts
import {Component, OnChanges, SimpleChanges, Input} from '@angular/core';
class Child implements OnInit, OnChanges {
@Input() Data: any; // here is the data variable which we are getting from the parent
constructor() {}
ngOnchanges(changes: SimpleChanges) {
console.log(changes); // here you will get the data from parent once the input param is change
}
}