Performance: Child to parent communication flow
This affects interaction responsiveness and rendering updates when child components send data or events to parent components.
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="clicked.emit()">Click</button>` }) export class ChildComp { @Output() clicked = new EventEmitter<void>(); } // Parent template: <child-comp (clicked)="parentMethod()"></child-comp>
import { Component, Input } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="notifyParent()">Click</button>` }) export class ChildComp { @Input() parentCallback!: () => void; notifyParent() { this.parentCallback(); } } // Parent template: <child-comp [parentCallback]="parentMethod"></child-comp>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing function via @Input | Triggers parent and child change detection | Multiple reflows if parent updates DOM | Higher paint cost due to unnecessary updates | [X] Bad |
| Using @Output EventEmitter | Triggers minimal change detection scoped to parent | Single reflow if DOM changes | Lower paint cost with targeted updates | [OK] Good |
@Output decorator with EventEmitter allows child to emit events the parent listens to.@Output() and initialized with new EventEmitter<type>().@Output() and initializes the emitter. Others miss decorator, use wrong decorator, or wrong syntax.@Output() change = new EventEmitter<number>();
updateValue() {
this.change.emit(42);
}updateValue() is called?emit method sends the given value (here 42) as an event payload to the parent.number and emits 42, so the parent receives 42.@Output notify = new EventEmitter<string>();
send() {
this.notify.emit('hello');
}@Output decorator requires parentheses: @Output().Child component:
@Output() sendData = new EventEmitter<{id:number, name:string}>();
onClick() {
const data = {id: 1, name: 'Test'};
this.sendData.emit(data);
}
Parent component template:
<app-child (sendData)="handleData($event)"></app-child>handleData method look like?id and name properties.name.Pass the test and this pattern moves unknown