Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda