0
0
Angularframework~20 mins

Child to parent communication flow in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Child-to-Parent Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a child component emits an event in Angular?
Consider a child component that uses @Output() with an EventEmitter. What is the expected behavior when the child emits an event?
Angular
Child component:
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `<button (click)="notifyParent()">Notify Parent</button>`
})
export class ChildComponent {
  @Output() notify = new EventEmitter<string>();

  notifyParent() {
    this.notify.emit('Hello from child');
  }
}

Parent component template:
<app-child (notify)="onNotify($event)"></app-child>
AThe parent component's <code>onNotify</code> method is called with the string 'Hello from child'.
BAngular throws an error because events cannot be emitted from child to parent.
CThe event is handled only inside the child component and does not affect the parent.
DThe child component directly changes the parent's state without any event.
Attempts:
2 left
💡 Hint
Think about how Angular uses @Output and EventEmitter to send data up.
📝 Syntax
intermediate
1:30remaining
Identify the correct syntax to declare an output event in Angular child component
Which of the following is the correct way to declare an output event named update in an Angular child component?
A@Output() update = new EventEmitter<string>();
B@Input() update = new EventEmitter<string>();
Coutput update = new EventEmitter<string>();
D@Output update = EventEmitter<string>();
Attempts:
2 left
💡 Hint
Remember the decorator name and how to instantiate EventEmitter.
🔧 Debug
advanced
2:30remaining
Why does the parent not receive the event from the child component?
Given this parent template and child component code, why does the parent not respond when the child emits the event? Parent template: Child component: @Output() dataChange = new EventEmitter(); someMethod() { this.dataChange.emit('data'); } But handleChange is never called.
AThe child component did not call <code>emit</code> inside <code>someMethod</code>.
BThe parent component forgot to import the child component.
CThe child component's event emitter property name does not match the event binding name in the parent template.
DAngular does not support event emitters for string data.
Attempts:
2 left
💡 Hint
Check if the event name in the template matches the property name in the child.
state_output
advanced
1:30remaining
What is the value of message in the parent after child emits?
Parent component code: message = ''; handleMessage(msg: string) { this.message = msg; } Parent template: Child component: @Output() sendMessage = new EventEmitter(); send() { this.sendMessage.emit('Hi Parent'); } If the child calls send(), what is the value of message in the parent?
Aundefined
B'Hi Parent'
C'' (empty string)
Dnull
Attempts:
2 left
💡 Hint
The parent updates message when it receives the event.
🧠 Conceptual
expert
3:00remaining
Which Angular feature enables child to parent communication?
In Angular, what is the main feature that allows a child component to send data or signals to its parent component?
AUsing services with <code>BehaviorSubject</code> only.
BUsing <code>@Input()</code> decorator to pass data from child to parent.
CDirectly modifying the parent's variables from the child component.
DUsing <code>@Output()</code> decorator with <code>EventEmitter</code> to emit events.
Attempts:
2 left
💡 Hint
Think about how Angular components send events upwards.