Search code examples
javascriptangulartypescriptangular7

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?


Solution

  • 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);