How to transfer data between two unrelated components in Angular

0 votes

How to transfer data between two unrelated components in Angular?

"I'm trying to figure out how to transfer data between two components in Angular, but the components are unrelated. One is a child component, and the other is a completely separate component, not nested or connected in any way. I know there are ways to share data between parent and child components, but I'm not sure how to do it when the components don't have a direct relationship.

Nov 26, 2024 in Angular by Nidhi
• 5,060 points
63 views

1 answer to this question.

0 votes

Steps to Transfer Data Between Unrelated Components

1. Create a Shared Service

The shared service acts as a bridge for data communication between the components.

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

import { BehaviorSubject } from 'rxjs';

@Injectable({

  providedIn: 'root'

})

export class DataService {

  private dataSource = new BehaviorSubject<any>(null); // Initial value is null

  currentData = this.dataSource.asObservable(); // Observable for components to subscribe

  // Method to update the data

  updateData(data: any) {

    this.dataSource.next(data);

  }

}

2. Provide the Service

Ensure the service is provided in the root module (done by providedIn: 'root' in the service). This makes it a singleton, accessible across the application.

3. Component A (Sender)

This component sends data using the updateData() method of the service.

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

import { DataService } from '../data.service';

@Component({

  selector: 'app-sender',

  template: `<button (click)="sendData()">Send Data</button>`,

})

export class SenderComponent {

  constructor(private dataService: DataService) {}

  sendData() {

    const data = { message: 'Hello from Sender Component!' };

    this.dataService.updateData(data); // Send data to the service

  }

}

4. Component B (Receiver)

This component subscribes to the currentData observable to receive updates.

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

import { DataService } from '../data.service';

@Component({

  selector: 'app-receiver',

  template: `<p>{{ receivedData?.message }}</p>`,

})

export class ReceiverComponent implements OnInit {

  receivedData: any;

  constructor(private dataService: DataService) {}

  ngOnInit() {

    this.dataService.currentData.subscribe((data) => {

      this.receivedData = data; // Update when data changes

    });

  }

}

answered Dec 12, 2024 by Navya

Related Questions In Angular

0 votes
1 answer

How to pass data from a child component to a parent component in Angular 4?

In Angular 4, passing data from a ...READ MORE

answered Dec 4, 2024 in Angular by Navya
92 views
0 votes
2 answers

How to detect a route change in Angular?

Hii Kartik For Angular 7 someone should write like: this.router.events.subscribe((event: Event) ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
29,309 views
0 votes
3 answers

How to load external scripts dynamically in Angular?

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

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
72,005 views
0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
981 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,268 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,867 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,778 views
0 votes
1 answer
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