Consider a child Angular component that uses @Output with EventEmitter. When the child emits an event, what will the parent component receive?
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-child', template: `<button (click)="notify()">Notify Parent</button>` }) export class ChildComponent { @Output() notifyEvent = new EventEmitter<string>(); notify() { this.notifyEvent.emit('Hello from child'); } }
Think about what emit() sends and how the parent listens.
The @Output decorator with EventEmitter allows the child to send data to the parent. When emit() is called with a value, the parent receives that exact value through event binding.
Which of the following is the correct way to declare an @Output property named changeEvent that emits numbers?
Remember the syntax requires parentheses after @Output and the type inside EventEmitter<>.
The correct syntax uses @Output() with parentheses and initializes the property with new EventEmitter<number>(). Option B matches this exactly.
Given the child component below, the parent component does not receive any event when the button is clicked. What is the most likely cause?
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-child', template: `<button (click)="send()">Send</button>` }) export class ChildComponent { @Output() notify = new EventEmitter<string>(); send() { this.notify.emit('data'); } }
Check how the parent binds to the child's output event name.
The child emits an event named 'notify'. If the parent listens to a different event name, it won't receive the event. The other options are syntactically correct.
In the parent component, the message property updates when the child emits an event. What will be the value of message after clicking the button?
/* Child component */ import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-child', template: `<button (click)="send()">Send</button>` }) export class ChildComponent { @Output() notify = new EventEmitter<string>(); send() { this.notify.emit('Angular Rocks!'); } } /* Parent component */ import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: `<app-child (notify)="message = $event"></app-child><p>{{ message }}</p>` }) export class ParentComponent { message = 'No message yet'; }
Consider what $event contains when the child emits.
The child emits the string 'Angular Rocks!'. The parent listens to the notify event and sets message to the emitted value, so it updates to 'Angular Rocks!'.
If you forget to import EventEmitter from @angular/core but still declare @Output() event = new EventEmitter();, what error will Angular throw during compilation?
Think about TypeScript errors when a class is used but not imported.
If EventEmitter is not imported, TypeScript cannot find its definition and throws error TS2304 during compilation.