7

I have service defined in Angular 2 like this:

import { Inject } from 'angular2/angular2';
import { Http ,Headers , HTTP_PROVIDERS } from 'angular2/http';

export interface CourseInterface {
    courseId: number,
    coursePrice: number,
    authorName: string
}

export class CourseDetailsService {
    http: Http;
    constructor(@Inject(Http) Http) {
        console.log(Http)
        this.http = Http;
    }

    load() {
        console.log("came here in service")
        var headers = new Headers();
        headers.append('Authorization', <my username password>);

        this.http.get('https://some.api',{
            headers : headers
        }).map(res => console.log("Response came!!!"))

        console.log("done . . .")
    }
}

and in another component, I use this service like this:

import {CourseInterface, CourseDetailsService} from '../services/course';

@Component({
    selector: 'dashboard',
    viewBindings: [CourseDetailsService]
})
@View({
    template: `
        <h1>Dashboard page laoded</h1>
  `
})
export class Dashboard {
    constructor(service: CourseDetailsService) {
        service.load();
    }
}

and while running the application, I can see my Dashboard component gets displayed on the screen. But however from the CourseDetailsService, no http calls are getting fired.

But in the console I could able to see the following printed:

came here in service
done . . . . 

But in my chrome networks tab, I couldn't able to see any request fired to the specified url. Where I am making mistake?

I'm using Angular 2 Alpha 47

3
  • The part that triggers the request it is the subscribe. So do return this.http.get().map(), and then in your component do service.load().subscribe((result) => ...). Commented Dec 5, 2015 at 11:22
  • @EricMartinez: So it basically returns an Observable? To which I need to subscribe and get my streams? Commented Dec 5, 2015 at 11:24
  • @Ants You are absolutely right! :D Commented Dec 5, 2015 at 11:31

1 Answer 1

8

Basically the part that triggers the request itself it's the subscribe, so to make it work you have to add it.

// Service
load() {
    var headers = new Headers();
    headers.append('Authorization', <my username password>);

    return this.http.get('https://some.api',{
        headers : headers
    }).map(res => console.log("Response came!!!"))
}

// Component
// 'subscribe' triggers the request!
service.load().subscribe((result) => /* do something with result */);
Sign up to request clarification or add additional context in comments.

5 Comments

:Totally out of this question, if say I have an array of unique request, like /something,/other, in those cases, how can I return the observables so that the consumer can capture them?
My approach would be to create separate methods like : loadSomething, loadOther, where the first case would do a request to https://some.api/something. The rest would look like the answer : service.loadSomething().subscribe(...). Does that make sense to you?
Another approach : add a parameter to load(), like load(query) { return http.get(query).subscribe(...)}. This approach is better than the above one.
can this approach make synchronous calls ? For example I need the server to validate the user rights for accessing a page and I would like this call to be synchronous and not add an *ngIf to not show the view until the call ends, I don't want to make a normal ajax request if I can use the http service
Why they made something such a simple and basic thing so complicated.

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.