2

I have a service class that should call an api and return the results:

import {Component, View} from 'angular2/angular2';
import { Inject} from 'angular2/di';
import {Http} from 'angular2/http';

var httpMap = new WeakMap<ScheduleService, Http>();
export class ScheduleService {
    meetings: Array<any>;
    http: any;

    constructor(@Inject(Http) http:Http){
        httpMap.set(this, http);
        this.http = http;

        //http.get('/api/sample')
        //   .map(response => response.json())
        //   .subscribe(data => {
        //      this.serverData = data;
        //      });
    }

    getMeetings(){
        var path = '/api/meetings/';
        return httpMap.get(this).get(path);
    }

}

The service class is being called and injected correctly. The issue I am having is that when ever I call the getMeetings method it never makes the request to /api/meetings. If you guys notice in the constructor there is a get request to /api/sample that works perfectly if I uncomment it and run the program I check my network tab and I can see the request was made.

2
  • 1
    My knowledge about weak references is really poor, so I couldn't answer why in that way is not working, so, why don't you just use this.http in your getMeetings() method? Commented Sep 23, 2015 at 22:03
  • @EricMartinez Its essentially the same thing. I was just looking at a few blogs that were doing the mapping I just thought it looked dirty so I tried setting it as a property to the object to see if it worked and it did Commented Sep 25, 2015 at 15:12

1 Answer 1

1

Looks like http.get(path) doesn't send off a request unless you call http.get(path).subscribe(); even if you don't do anything with the response.

Plunker: https://plnkr.co/edit/b1fxoIUy31EHTC84U344?p=preview

In the Plunker, open the network view in the console and click the With Subscribe / Without Subscribe buttons for comparison.

Sign up to request clarification or add additional context in comments.

1 Comment

Observables are lazy and don't do anything before subscribe() or toPromise() is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.