Performance: @Output decorator with EventEmitter
This affects interaction responsiveness and event handling efficiency between Angular components.
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="emitEvent()">Click</button>` }) export class ChildComponent { @Output() dataChange = new EventEmitter<number>(); private lastEmittedTime = 0; emitEvent() { const now = Date.now(); if (now - this.lastEmittedTime > 1000) { // throttle events this.dataChange.emit(now); this.lastEmittedTime = now; } } }
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="emitEvent()">Click</button>` }) export class ChildComponent { @Output() dataChange = new EventEmitter<any>(); emitEvent() { // Emitting event with new object every click this.dataChange.emit({ time: Date.now() }); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Emit event every click with new object | Multiple parent updates | Multiple reflows per event | High paint cost due to frequent updates | [X] Bad |
| Throttle event emissions to reduce frequency | Fewer parent updates | Single or fewer reflows | Lower paint cost | [OK] Good |
@Output decorator in Angular?@Output decorator is used in Angular to create a custom event that a child component can emit to notify its parent component.@Input which receives data, @Output sends events upward. It does not style or import modules.@Output property named notify using EventEmitter in Angular?@Output() decorator with a property initialized as new EventEmitter<type>().@Output and calls new EventEmitter<string>(). Others miss parentheses or use wrong initialization.import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Click` })
export class ChildComponent {
@Output() alertEvent = new EventEmitter<string>();
sendAlert() {
this.alertEvent.emit('Hello from child');
}
}<child-comp (alertEvent)="showMessage($event)"></child-comp>?alertEvent with the string 'Hello from child'.alertEvent and calls showMessage passing the emitted string as $event.import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Notify` })
export class ChildComponent {
@Output notify = new EventEmitter<string>();
notifyParent() {
this.notify.emit('Data sent');
}
}@Output decorator must have parentheses: @Output(). Missing parentheses causes a syntax error.@Output and EventEmitter?import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Send` })
export class ChildComponent {
@Output() positiveNumber = new EventEmitter<number>();
numberInput = 0;
checkAndEmit() {
if (this.numberInput > 0) {
this.positiveNumber.emit(this.numberInput);
}
}
} emits only if numberInput > 0, which matches the requirement. import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Send` })
export class ChildComponent {
@Output() positiveNumber = new EventEmitter<number>();
numberInput = 0;
emitNumber() {
this.positiveNumber.emit(this.numberInput);
}
} emits unconditionally. import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Send` })
export class ChildComponent {
@Output() positiveNumber = new EventEmitter<number>();
numberInput = 0;
send() {
if (this.numberInput >= 0) {
this.positiveNumber.emit(this.numberInput);
}
}
} emits if number is zero or positive (>=0), which is incorrect. import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Send` })
export class ChildComponent {
@Output() positiveNumber = new EventEmitter<number>();
numberInput = 0;
sendPositive() {
if (this.numberInput < 0) {
this.positiveNumber.emit(this.numberInput);
}
}
} emits if number is negative (<0), which is wrong.Pass the test and this pattern moves unknown