angular - How to use EventEmitter to pass function to a child component?

Angular - How to use EventEmitter to pass function to a child component?

To pass a function from a parent component to a child component using EventEmitter in Angular, follow these steps:

Step 1: Create the Parent Component

In the parent component, define a method you want to pass to the child. Use EventEmitter to emit events back to the parent.

parent.component.ts:

import { Component } from '@angular/core'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', }) export class ParentComponent { parentFunction() { console.log('Function called from child component!'); } } 

parent.component.html:

<app-child [functionToCall]="parentFunction"></app-child> 

Step 2: Create the Child Component

In the child component, use an @Input to receive the function from the parent and call it when needed.

child.component.ts:

import { Component, Input } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', }) export class ChildComponent { @Input() functionToCall!: () => void; callParentFunction() { if (this.functionToCall) { this.functionToCall(); } } } 

child.component.html:

<button (click)="callParentFunction()">Call Parent Function</button> 

Summary

  1. Define the Function: Create the function in the parent component that you want to pass.
  2. Pass the Function: Use property binding in the parent's template to pass the function to the child.
  3. Receive and Call: In the child component, use @Input to receive the function and call it when needed (e.g., on a button click).

This approach allows you to effectively communicate between components in Angular, enabling a clear flow of data and actions.

Examples

  1. How to pass a function from parent to child component using EventEmitter in Angular?

    • Description: Use EventEmitter to define and pass a function from a parent component to a child component in Angular.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="onChildEvent($event)"></app-child> ` }) export class ParentComponent { onChildEvent(message: string) { console.log(`Message from child: ${message}`); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="sendMessage()">Send Message to Parent</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter<string>(); sendMessage() { this.childEvent.emit('Hello from child!'); } } 
  2. Angular EventEmitter pass function to child component example

    • Description: Demonstrate how to use EventEmitter to pass a function as an event handler from parent to child component.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="handleChildEvent($event)"></app-child> ` }) export class ParentComponent { handleChildEvent(event: any) { console.log('Received event in parent:', event); } parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="emitEvent()">Call Parent Function</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter(); emitEvent() { this.childEvent.emit(); } } 
  3. Angular pass function to child component and emit event

    • Description: Pass a parent function to a child component in Angular and emit an event to invoke it.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child [childFunction]="parentFunction"></app-child> ` }) export class ParentComponent { parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, Input } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="childFunction()">Call Parent Function</button> ` }) export class ChildComponent { @Input() childFunction: Function; childFunction() { this.childFunction(); } } 
  4. Angular EventEmitter example to pass function to child component

    • Description: Example of using EventEmitter in Angular to pass a function from parent to child component.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="handleEvent($event)"></app-child> ` }) export class ParentComponent { handleEvent(event: string) { console.log('Received event:', event); } parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="emitEvent()">Invoke Parent Function</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter<string>(); emitEvent() { this.childEvent.emit('Hello from child!'); } } 
  5. Angular 11 EventEmitter pass function to child component

    • Description: Implement EventEmitter in Angular version 11 to pass a function to a child component.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="handleChildEvent($event)"></app-child> ` }) export class ParentComponent { handleChildEvent(message: string) { console.log(`Message received from child: ${message}`); } parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="sendMessage()">Send Message to Parent</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter<string>(); sendMessage() { this.childEvent.emit('Hello from child!'); } } 
  6. How to emit function from parent to child component using EventEmitter in Angular?

    • Description: Emit a function from a parent component to a child component using Angular's EventEmitter.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childFunction)="parentFunction()"></app-child> ` }) export class ParentComponent { parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="emitFunction()">Call Parent Function</button> ` }) export class ChildComponent { @Output() childFunction = new EventEmitter(); emitFunction() { this.childFunction.emit(); } } 
  7. Angular pass function as prop to child component and emit event

    • Description: Pass a function as a prop to a child component in Angular and emit an event to invoke it.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child [childFunction]="parentFunction"></app-child> ` }) export class ParentComponent { parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, Input } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="childFunction()">Call Parent Function</button> ` }) export class ChildComponent { @Input() childFunction: Function; childFunction() { this.childFunction(); } } 
  8. Angular 9 EventEmitter pass function to child component example

    • Description: Example of using EventEmitter in Angular version 9 to pass a function to a child component.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="handleChildEvent($event)"></app-child> ` }) export class ParentComponent { handleChildEvent(event: string) { console.log('Received event:', event); } parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="emitEvent()">Invoke Parent Function</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter<string>(); emitEvent() { this.childEvent.emit('Hello from child!'); } } 
  9. Angular pass function to child component and emit event

    • Description: Pass a function to a child component in Angular and emit an event to trigger it.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="handleChildEvent($event)"></app-child> ` }) export class ParentComponent { handleChildEvent(message: string) { console.log(`Message received from child: ${message}`); } parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="sendMessage()">Send Message to Parent</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter<string>(); sendMessage() { this.childEvent.emit('Hello from child!'); } } 
  10. How to use EventEmitter to pass a function from parent to child component in Angular 10?

    • Description: Implement EventEmitter in Angular version 10 to pass a function as an event handler from parent to child component.

    • Code Example:

      Parent Component (parent.component.ts):

      import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <app-child (childEvent)="handleEvent($event)"></app-child> ` }) export class ParentComponent { handleEvent(event: string) { console.log('Received event:', event); } parentFunction() { console.log('Parent function called.'); } } 

      Child Component (child.component.ts):

      import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: ` <button (click)="emitEvent()">Invoke Parent Function</button> ` }) export class ChildComponent { @Output() childEvent = new EventEmitter<string>(); emitEvent() { this.childEvent.emit('Hello from child!'); } } 

More Tags

spring-cloud-gateway nexus json-serialization servlet-filters glsl django-templates mnist fragment-tab-host strftime grand-central-dispatch

More Programming Questions

More Livestock Calculators

More Fitness Calculators

More Dog Calculators

More Internet Calculators