0

I want to subscribe to observable from http.get without triggering it because i want it to be triggered by some other commponent.

Currently, I am doing this:

shared.service.ts

    getAllIcons(){
        console.log('getting all icons');
        return this.http.get(`http://localhost:3000/AllIcons`).map(function (response: Response) {
          return response.json();
        })
    }

component1

this.sharedService.getAllIcons().subscribe(
      (value) =>{console.log(value);});
  }


what I want to achieve

shared.service.ts

  _observableForAllImages: Observable<{}>;
    getAllIcons(){
        console.log('getting all icons');
        return _observableForAllImages =  this.http.get(`http://localhost:3000/AllIcons`).map(function (response: Response) {
          return response.json();
        })
    }

component2

this.sharedService.getAllIcons().subscribe(
      (value) =>{console.log(value);});
  }

component1

//BUT THIS GIVES ERROR
    this.sharedService._observableForAllImages.subscribe((value)=>{
          console.log(value);
         this.imageContainers = value.imageContainers;
        });
**ERROR =  TypeError: Cannot read property 'subscribe' of undefined**

Why do I want it?

I have a component called imageGridComponent. This component is called by various parent component using routing and imageGridComponent displays all the images corresponding to that parent. Therefore, I want parent to be the triggering component and child to be the data using component.

Edit: My question is not about sharing the result of a http call to various subscriptions. Rather its about a single subscription (in child component) receiving data from the service method which will be called by different parents. I have attached a schematic to explain it.

enter image description here

dasfdsa
  • 7,102
  • 8
  • 51
  • 93
  • In your component1 you are subscribing to this.sharedService._observable however in your shared service the variable is named '_observableForAllImages'. Could that be the issue? – JeanPaul A. Aug 04 '17 at 08:52
  • When are you calling your example in component1? in constructor? – Supamiu Aug 04 '17 at 09:22
  • Looks like a dup of https://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-call-in/36291681#36291681 to me – Günter Zöchbauer Aug 04 '17 at 09:24
  • @Supamiu in ngOnInit() – dasfdsa Aug 04 '17 at 09:25
  • @JeanPaulA.Sorry that was my mistake while typing the question. I have edited in question – dasfdsa Aug 04 '17 at 09:26
  • @GünterZöchbauer I agree with you, possible duplicate as the answer would be the same. – Supamiu Aug 04 '17 at 09:29
  • Possible duplicate of [What is the correct way to share the result of an Angular 2 Http network call in RxJs 5?](https://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-call-in) – Supamiu Aug 04 '17 at 09:33
  • @Supamiu Can you explain why I am getting that error when subscribing to uninitialized Observable? – dasfdsa Aug 04 '17 at 09:33
  • That's probably because the call from component1 is made before the one from component2, since you initialize the Observable in component2's call, it's undefined before. – Supamiu Aug 04 '17 at 09:35

0 Answers0