What is the purpose of the @Output decorator in Angular?
The @Output decorator allows a child component to send data or events to its parent component. It marks a property as an event that the parent can listen to.
Click to reveal answer
beginner
What role does EventEmitter play when used with @Output?
EventEmitter is a class that creates an event stream. When used with @Output, it lets the child component emit events that the parent component can subscribe to and react.
Click to reveal answer
beginner
How do you emit an event from a child component using EventEmitter?
You call the emit() method on the EventEmitter instance, passing the data you want to send to the parent. For example: this.myEvent.emit(data);
Click to reveal answer
beginner
In the parent component template, how do you listen to an event emitted by a child component's @Output property?
You use event binding syntax with parentheses around the event name, like . The $event holds the emitted data.
Click to reveal answer
intermediate
Why is using @Output with EventEmitter better than directly accessing child component properties?
It keeps components loosely coupled and follows Angular's unidirectional data flow. The child only emits events, and the parent decides how to handle them, improving maintainability.
Click to reveal answer
What does the @Output decorator do in Angular?
AAllows a child component to send events to its parent
BImports external modules
CDefines a service
DCreates a new component
✗ Incorrect
The @Output decorator marks a property as an event that a child component can emit to notify its parent.
Which class is used with @Output to emit events?
APromise
BObservable
CEventEmitter
DSubject
✗ Incorrect
EventEmitter is the class used to create events that can be emitted from child to parent.
How do you emit an event named 'update' with data '42' from a child component?
Athis.update.emit(42);
Bemit.update(42);
Cthis.emit(update, 42);
Dupdate.emitEvent(42);
✗ Incorrect
You call the emit() method on the EventEmitter property, passing the data.
In the parent template, how do you listen to a child's @Output event named 'change'?
A<child {change}="onChange($event)"></child>
B<child [change]="onChange($event)"></child>
C<child #change="onChange($event)"></child>
D<child (change)="onChange($event)"></child>
✗ Incorrect
Parent listens to child events using parentheses for event binding.
Why should components use @Output and EventEmitter instead of directly accessing child properties?
ATo make components dependent on each other
BTo keep components loosely coupled and maintain unidirectional data flow
CTo increase code complexity
DTo avoid using Angular features
✗ Incorrect
Using @Output keeps components independent and follows Angular's design principles.
Explain how a child component can send data to its parent using @Output and EventEmitter in Angular.
Think about how events flow from child to parent.
You got /4 concepts.
Describe why using @Output with EventEmitter is important for component communication in Angular.
Consider the benefits of clear communication paths.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Output decorator in Angular?
easy
A. To allow a child component to send events to its parent component
B. To receive data from a parent component
C. To style the component's template
D. To import external modules into the component
Solution
Step 1: Understand the role of @Output
The @Output decorator is used in Angular to create a custom event that a child component can emit to notify its parent component.
Step 2: Differentiate from other decorators
Unlike @Input which receives data, @Output sends events upward. It does not style or import modules.
Final Answer:
To allow a child component to send events to its parent component -> Option A
Quick Check:
@Output sends events from child to parent [OK]
Hint: Remember: @Output sends events up from child to parent [OK]
Common Mistakes:
Confusing @Output with @Input
Thinking @Output styles components
Assuming @Output imports modules
2. Which of the following is the correct syntax to declare an @Output property named notify using EventEmitter in Angular?
easy
A. @Output() notify = new EventEmitter;
B. @Output notify = EventEmitter<string>();
C. @Output() notify = new EventEmitter<string>();
D. @Output() notify: EventEmitter<string> = EventEmitter();
Solution
Step 1: Recall correct EventEmitter syntax
The correct way is to use @Output() decorator with a property initialized as new EventEmitter<type>().
Step 2: Check each option
@Output() notify = new EventEmitter<string>(); correctly uses parentheses after @Output and calls new EventEmitter<string>(). Others miss parentheses or use wrong initialization.
Final Answer:
@Output() notify = new EventEmitter<string>(); -> Option C
Quick Check:
Use @Output() with new EventEmitter() [OK]
Hint: Always use parentheses: @Output() and new EventEmitter() [OK]
Common Mistakes:
Omitting parentheses after @Output
Not using 'new' keyword with EventEmitter
Incorrect property type declaration
3. Given this child component code:
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');
}
}
What happens when the button is clicked in the parent template listening as <child-comp (alertEvent)="showMessage($event)"></child-comp>?
medium
A. The child component reloads
B. Nothing happens because alertEvent is private
C. An error occurs because emit is not a function
D. The parent method showMessage is called with 'Hello from child' as argument
Solution
Step 1: Understand EventEmitter usage
The child component emits an event named alertEvent with the string 'Hello from child'.
Step 2: Parent listens to event
The parent template listens to alertEvent and calls showMessage passing the emitted string as $event.
Final Answer:
The parent method showMessage is called with 'Hello from child' as argument -> Option D
Quick Check:
Child emits event, parent handles it [OK]
Hint: Child emits event, parent listens with (eventName) [OK]
Common Mistakes:
Thinking emit is private or inaccessible
Assuming child reloads on emit
Confusing emit with method call error
4. Identify the error in this child component code:
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');
}
}
medium
A. EventEmitter should not have a type parameter
B. Missing parentheses after @Output decorator
C. Method notifyParent should return a value
D. Template syntax for click event is incorrect
Solution
Step 1: Check @Output decorator syntax
The @Output decorator must have parentheses: @Output(). Missing parentheses causes a syntax error.
Step 2: Verify other parts
EventEmitter can have a type parameter, method does not need to return, and template click syntax is correct.
Final Answer:
Missing parentheses after @Output decorator -> Option B
Quick Check:
@Output() needs parentheses [OK]
Hint: Always write @Output() with parentheses [OK]
Common Mistakes:
Omitting parentheses after @Output
Thinking EventEmitter cannot have type
Expecting method to return value unnecessarily
5. You want a child component to emit an event only when a number input is positive. Which code correctly implements this using @Output and EventEmitter?
hard
A.
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);
}
}
}