Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
How to pass data from one component to another dynamically without "Parent-Child" relationship in Angular 2+?

This is somewhat an open discussion but I'm really curious about passing data dynamically from one component to another.

I know it can be done using @Input() decorator. But it has its disadvantages like you can't use the back button on your browser. Because if you do, then you will do back an entire page. It won't work in the Parent-Child method because we're using ng-If to hide the child directives on a page and It won't update the content dynamically unless you reload that page. And when you reload that page, you lose the current state of the UI.

If the data is static then there's absolutely no problem but when it's dynamic and you try to pass the data, it shows as undefined.

I would appreciate any pointers on how would I be able to achieve that.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Another way is using a service. You have to use the service as a "store" of your data values:

  1. Create a service:
ng g s MyServiceName --skip-Tests

Once it is created, check that has been decorated as providedIn root:

@Injectable({
  providedIn: 'root',
})
export class MyServiceNameService {
....
  1. In your service, declare all data you want the components share (have in commun), and create methods setters/getters for this data. e.g:
private _myVariable: string;

get myVariable(){
   return _myVariable;
}

set myVariable(newValue: string){
   this._myVariable = newValue;
}

constructor(){
   _myVariable = "exampletest";
}



  1. Inject the service in the constructor of your components (in parent and in child):
...
import { MyServiceNameService } from '../../services/my-service-name.service';
...
constructor(
   private myServiceNameService : MyServiceNameService ) {
  }
...
  1. Used directly the variables in the service through methods in the service to modify the varaibles values or get its values.

e.g: in components:

let myVariable:string;

....
getNewValue(){
   myVariable = this.myServiceNameService.myVariable;
}
setNewValue ( newValue: string) {
   this.myServiceNameService.myVariable(newValue);
}

over 4 years ago · Santiago Trujillo Report

0

There could be multiple ways here:

  1. You can create angular service (use @Injectable() annotation to create service). Angular service follows singleton design pattern and can be shared among multiple pages of your application. Wherever you need, just inject that service and you can access all properties/methods.
    Link how to create angular service -

    https://angular.io/guide/architecture-services

  2. You can use redux concept and create a store having data & then that data could be accessible across all pages. NgRx is the best library with Angular to create store and access data wherever needed. NgRx link -

    https://ngrx.io/guide/store

  3. You can use RxJs library to share data between peer components.
    RxJs link -

    https://rxjs.dev/guide/overview

over 4 years ago · Santiago Trujillo Report

0

Use a shared service and create an observable inside the service, then inject this service to the components need to access this shared data and create a subscription on each component. When ever the data in the observable changes the subscribed components will get notified.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!