0
0
Angularframework~20 mins

@Output decorator with EventEmitter in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the child component emits an event?

Consider a child Angular component that uses @Output with EventEmitter. When the child emits an event, what will the parent component receive?

Angular
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');
  }
}
AThe parent receives no data because EventEmitter does not send data by default.
BThe parent receives an object with a property named 'notifyEvent'.
CThe parent receives the string 'Hello from child' through the event binding.
DThe parent receives a boolean value true indicating the event fired.
Attempts:
2 left
💡 Hint

Think about what emit() sends and how the parent listens.

📝 Syntax
intermediate
1:30remaining
Identify the correct syntax to declare an @Output EventEmitter

Which of the following is the correct way to declare an @Output property named changeEvent that emits numbers?

A@Output changeEvent = EventEmitter<number>();
B@Output() changeEvent = new EventEmitter<number>();
C@Output() changeEvent: EventEmitter = new EventEmitter();
D@Output() changeEvent = new EventEmitter<string>();
Attempts:
2 left
💡 Hint

Remember the syntax requires parentheses after @Output and the type inside EventEmitter<>.

🔧 Debug
advanced
2:30remaining
Why does the parent not receive the event?

Given the child component below, the parent component does not receive any event when the button is clicked. What is the most likely cause?

Angular
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');
  }
}
AThe @Output decorator is missing parentheses.
BThe EventEmitter is not initialized with parentheses after new.
CThe child component's selector is incorrect.
DThe parent is not listening to the event with the correct selector name.
Attempts:
2 left
💡 Hint

Check how the parent binds to the child's output event name.

state_output
advanced
2:00remaining
What is the value of the parent's message after event emission?

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?

Angular
/* 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';
}
A'Angular Rocks!'
B'No message yet'
Cundefined
DAn empty string ''
Attempts:
2 left
💡 Hint

Consider what $event contains when the child emits.

🧠 Conceptual
expert
1:30remaining
What error occurs if EventEmitter is not imported?

If you forget to import EventEmitter from @angular/core but still declare @Output() event = new EventEmitter();, what error will Angular throw during compilation?

ATS2304: Cannot find name 'EventEmitter'.
BTemplate parse error: 'event' is not a known property.
CNo error; Angular automatically imports EventEmitter.
DRuntime error: 'event' is not a function.
Attempts:
2 left
💡 Hint

Think about TypeScript errors when a class is used but not imported.