Bird
Raised Fist0
Angularframework~5 mins

Child to parent communication flow in Angular

Choose your learning style10 modes available
Introduction

Sometimes a child component needs to send information back to its parent. This helps the parent know what the child did or wants.

When a button click inside a child component should trigger an action in the parent.
When a form inside a child component submits data that the parent needs to process.
When a child component changes a value and the parent needs to update something based on it.
When you want to keep the parent informed about events happening inside the child.
Syntax
Angular
In child component:
@Output() eventName = new EventEmitter<type>();

To send data:
this.eventName.emit(data);

In parent template:
<child-component (eventName)="parentHandler($event)"></child-component>

In parent component:
parentHandler(data: type) {
  // handle data
}

The @Output decorator marks an event the child can send out.

The parent listens to this event using parentheses (eventName) in the template.

Examples
The child sends a string message to the parent, which logs it.
Angular
Child component:
@Output() notify = new EventEmitter<string>();

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

Parent template:
<app-child (notify)="receiveMessage($event)"></app-child>

Parent component:
receiveMessage(msg: string) {
  console.log(msg);
}
The child sends a number to the parent whenever the count changes.
Angular
Child component:
@Output() countChanged = new EventEmitter<number>();

increment() {
  this.count++;
  this.countChanged.emit(this.count);
}

Parent template:
<counter (countChanged)="updateCount($event)"></counter>

Parent component:
updateCount(newCount: number) {
  this.currentCount = newCount;
}
Sample Program

The child component has a button. When clicked, it sends a string to the parent using an event. The parent listens and shows the received data below the child.

Angular
/* child.component.ts */
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `<button (click)="sendData()">Send Data</button>`
})
export class ChildComponent {
  @Output() dataSent = new EventEmitter<string>();

  sendData() {
    this.dataSent.emit('Data from child');
  }
}

/* parent.component.ts */
import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `
    <h2>Parent Component</h2>
    <app-child (dataSent)="handleData($event)"></app-child>
    <p>Received: {{ receivedData }}</p>
  `
})
export class ParentComponent {
  receivedData = '';

  handleData(data: string) {
    this.receivedData = data;
  }
}
OutputSuccess
Important Notes

Always import EventEmitter and @Output from @angular/core.

The parent uses $event to get the data sent by the child.

Use simple data types or objects for the event payload to keep communication clear.

Summary

Child to parent communication uses @Output and EventEmitter.

The child emits events, and the parent listens and reacts.

This keeps components loosely connected and easy to manage.

Practice

(1/5)
1. What is the main Angular feature used for communication from a child component to its parent?
easy
A. Using ViewChild to access parent methods
B. Using @Input to send data
C. Using services with HttpClient
D. Using @Output with EventEmitter

Solution

  1. Step 1: Understand child to parent communication

    Child components send data or events to parents using Angular's event system.
  2. Step 2: Identify Angular feature for this

    @Output decorator with EventEmitter allows child to emit events the parent listens to.
  3. Final Answer:

    Using @Output with EventEmitter -> Option D
  4. Quick Check:

    Child to parent = @Output + EventEmitter [OK]
Hint: Child emits events with @Output and EventEmitter [OK]
Common Mistakes:
  • Confusing @Input as child to parent communication
  • Thinking services are needed for simple child to parent
  • Using ViewChild to call parent methods directly
2. Which of the following is the correct syntax to declare an event emitter in a child component?
easy
A. notify = new EventEmitter<string>();
B. @Input() notify = new EventEmitter<string>();
C. @Output() notify = new EventEmitter<string>();
D. @Output notify = EventEmitter<string>();

Solution

  1. Step 1: Recall correct decorator and initialization

    Event emitters must be decorated with @Output() and initialized with new EventEmitter<type>().
  2. Step 2: Check each option

    @Output() notify = new EventEmitter<string>(); correctly uses @Output() and initializes the emitter. Others miss decorator, use wrong decorator, or wrong syntax.
  3. Final Answer:

    @Output() notify = new EventEmitter<string>(); -> Option C
  4. Quick Check:

    @Output() + new EventEmitter() = correct syntax [OK]
Hint: Always use @Output() with new EventEmitter() [OK]
Common Mistakes:
  • Using @Input instead of @Output
  • Forgetting parentheses after @Output
  • Not initializing EventEmitter with new keyword
3. Given this child component code:
@Output() change = new EventEmitter<number>();
updateValue() {
  this.change.emit(42);
}

What will the parent component receive when updateValue() is called?
medium
A. An event with value 42
B. An event with value 'updateValue'
C. No event emitted
D. An error because emit needs a string

Solution

  1. Step 1: Understand what emit does

    The emit method sends the given value (here 42) as an event payload to the parent.
  2. Step 2: Check the emitted value type

    The emitter is typed as number and emits 42, so the parent receives 42.
  3. Final Answer:

    An event with value 42 -> Option A
  4. Quick Check:

    emit(42) sends 42 to parent [OK]
Hint: emit(value) sends that value to parent [OK]
Common Mistakes:
  • Assuming emit sends method name instead of value
  • Thinking emit requires string only
  • Believing no event is sent without explicit subscription
4. Identify the error in this child component code snippet:
@Output notify = new EventEmitter<string>();
send() {
  this.notify.emit('hello');
}
medium
A. Missing parentheses after @Output decorator
B. EventEmitter should not have a type parameter
C. emit method cannot send strings
D. notify should be private

Solution

  1. Step 1: Check @Output decorator syntax

    The @Output decorator requires parentheses: @Output().
  2. Step 2: Verify other parts

    Type parameter on EventEmitter is allowed, emit can send strings, and notify can be public.
  3. Final Answer:

    Missing parentheses after @Output decorator -> Option A
  4. Quick Check:

    @Output() needs parentheses [OK]
Hint: Always write @Output() with parentheses [OK]
Common Mistakes:
  • Omitting parentheses after @Output
  • Thinking EventEmitter can't have types
  • Assuming emit only accepts numbers
5. You want a child component to send a complex object to its parent when a button is clicked. Which approach correctly implements this?
Child component:
@Output() sendData = new EventEmitter<{id:number, name:string}>();

onClick() {
  const data = {id: 1, name: 'Test'};
  this.sendData.emit(data);
}

Parent component template:
<app-child (sendData)="handleData($event)"></app-child>

What should the parent component's handleData method look like?
hard
A. handleData(data: number) { console.log(data); }
B. handleData(data: {id: number; name: string}) { console.log(data.name); }
C. handleData(event: string) { console.log(event); }
D. handleData() { console.log('No data received'); }

Solution

  1. Step 1: Understand emitted data type

    The child emits an object with id and name properties.
  2. Step 2: Match parent handler parameter

    The parent method must accept an object matching the emitted type to access properties like name.
  3. Final Answer:

    handleData(data: {id: number; name: string}) { console.log(data.name); } -> Option B
  4. Quick Check:

    Parent handler param matches emitted object type [OK]
Hint: Parent handler param matches emitted object shape [OK]
Common Mistakes:
  • Ignoring event parameter in handler
  • Using wrong parameter type in handler
  • Not accessing object properties correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard