trx
trx

Reputation: 2167

Trying to pass the data between the component using the service

I am using service to pass the data between the component and my component looks like below

  constructor(private psService: ProjectShipmentService, private pdComp: ProjectDetailsComponent) {

  }
  ngOnInit() {
    this.psService.getTDate().subscribe(x => this.cachedResults = x);
    this.populateArrays();

where my service is like

 constructor(private service: DataService, private pInfo: ProjectDetailsComponent) {

    this.rProjectNumber = this.pInfo.rProjectNumber;
    this.rProjectSO = this.pInfo.rSalesOrder;
    this.entityUrl = this.pInfo.entityUrl;
    this.tDate = service.get<ShipDateFilterModel[]>(this.entityUrl);
   }

Eventhough the tDate has data when subscribing it in the Component has no data. this.cachedResults is empty when the service is called on the ngOnInIt lifecycle hook. What am I missing here?

Upvotes: 0

Views: 72

Answers (2)

Manish
Manish

Reputation: 5066

Try it this way..

export class ProjectShipmentService {
  rProjectNumber;
  tDate: Observable<ShipDateFilterModel[]>;
  entityUrl;
  rProjectSO;

  constructor(private service: DataService, private pInfo: ProjectDetailsComponent) {
    this.rProjectNumber = this.pInfo.rProjectNumber;
    this.rProjectSO = this.pInfo.rSalesOrder;
    this.entityUrl = this.pInfo.entityUrl;
   }

getTDate(){
    return this.service.get<ShipDateFilterModel[]>(this.entityUrl);
}

And in your component.

  ngOnInit() {
    this.psService.getTDate.subscribe(x => this.cachedResults = x);
    this.populateArrays();
  }

Hope this helps :)

Upvotes: 1

jitender
jitender

Reputation: 10429

Why don't you simple create a method in service like

getTDate(){
return this.service.get<ShipDateFilterModel[]>(this.entityUrl);
}

And subscribe in your component

this.psService.getTDate().subscribe(x => this.cachedResults = x);

Upvotes: 1

Related Questions