HCP
HCP

Reputation: 43

Sending data from one component to another

Bonjour,

Here is the component tree of my application:

--data
  --graphic

--assetsStatus
  --location.compenent
  --vente.compenent

So I would like to send data from the assetsStatus components to the component data to feed a graph.

Thank you

Upvotes: 0

Views: 58

Answers (1)

Cagri Tacyildiz
Cagri Tacyildiz

Reputation: 17600

If the components are unrelated , then you can use service. If your components have parent child relationship then you can use @Input @Output way.

For your scenario you can use one generic service.

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class ShareService  {   
  constructor() { } 
  private paramSource = new BehaviorSubject("");// inside bracket you can send any type
  sharedData = this.paramSource.asObservable();
  setParam(param:string) { this.paramSource.next(param)}    
}

add to providers in appmodule providers:[ShareService ]

set in constructors.

constructor(private shareService: ShareService  ){}

to set in service

this.shareService.setParam('Sending param');

to get from service

 this.shareService.sharedData.subscribe(data=> { console.log(data); })

Upvotes: 2

Related Questions