How to update data between parent and child components in Angular

0 votes

How to update data between parent and child components in Angular?

I’m working on an Angular project where I need to update and share data between a parent and a child component. I’ve heard about @Input(), @Output(), and services, but I’m not entirely sure which method is best or how to implement it properly. Can someone explain the approach to handle this?

Nov 26 in Web Development by Nidhi
• 3,820 points
30 views

1 answer to this question.

0 votes

Data between parent and child components can be managed using @Input and @Output decorators.

1. Using @Input()

This allows a parent component to bind data to a child component.

Parent Component Template:

<app-child [childData]="parentData"></app-child>

Child Component Class:

import { Component, Input } from '@angular/core';

@Component({

  selector: 'app-child',

  template: `...`

})

export class ChildComponent {

  @Input() childData: any;

}

In this example, parentData from the parent component is passed to childData in the ChildComponent.

2. Using @Output()

This allows a child component to send data to a parent component through event emitters.

Child Component Class:

import { Component, Output, EventEmitter } from '@angular/core';

@Component({

  selector: 'app-child',

  template: `

    <button (click)="sendDataToParent()">Send Data</button>

  `

})

export class ChildComponent {

  @Output() dataEmitter = new EventEmitter<any>();


  sendDataToParent() {

    const data = { message: 'Hello Parent!' };

    this.dataEmitter.emit(data);

  }

}

Parent Component Template:

<app-child (dataEmitter)="receiveData($event)"></app-child>

Parent Component Class:

import { Component } from '@angular/core';


@Component({

  selector: 'app-parent',

  template: `...`

})

export class ParentComponent {

  receiveData(data: any) {

    console.log('Data from child received:', data);

  }

}

In this setup, when the button in the child component is clicked, it emits data to the parent component, which can then handle it via the receiveData function.

answered Nov 27 by kavya

Related Questions In Web Development

0 votes
1 answer

How to update Angular version in a project?

Angular is a powerful framework for building ...READ MORE

answered Nov 4 in Web Development by kavya
108 views
0 votes
1 answer
0 votes
1 answer

How to load external scripts dynamically in Angular?

Hello @kartik, You can use following technique to ...READ MORE

answered Sep 8, 2020 in Web Development by Niroj
• 82,840 points
5,435 views
0 votes
0 answers

How to filter JSON Data in JavaScript or jQuery?

How to filter JSON data using Javascript ...READ MORE

Jul 1, 2022 in Web Development by rajatha
• 7,680 points
2,489 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
13,176 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
1,775 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,678 views
0 votes
1 answer

How to use ngIf in Angular?

Nglf is a structural directive that conditionally ...READ MORE

answered Nov 13 in Web Development by kavya
137 views
0 votes
1 answer

What are the differences between Subject, BehaviorSubject, and ReplaySubject in Angular?

In Angular, Subject, Behaviour Subject, and Replay ...READ MORE

answered Nov 27 in Web Development by kavya
37 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP