0

Can't seem to figure this out. I have a custom class that extends the built-in angular Http class.

import { Injectable } from '@angular/core';
import {
    Http,
    ConnectionBackend,
    RequestOptions,
    RequestOptionsArgs,
    Request,
    Response
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { OAuth } from './oauth.service';

@Injectable()
export class HttpService extends Http {
    constructor(connectionBackend: ConnectionBackend, defaultOptions: RequestOptions, private $oauth: OAuth) {
        super(connectionBackend, defaultOptions);
        debugger;
    }    
}

I then inject my HttpService into another class.

import { HttpService } from './http.service';
import { Injectable } from '@angular/core';


@Injectable()
export class KateService {
    constructor(private $http: HttpService) {

    }
}

However, at the debugger statement that I have set in my HttpService, my private $oauth: OAuth is null. And when I look at the callstack, the calling method doesn't inject my OAuth service, just the two (connectionBackend, and defaultOptions).

I feel that it's treating my custom Http service like the built-in angular Http service. But I'm pretty new to Angular2 so...

1 Answer 1

1

You need to provide HTTP_PROVIDERS (import HttpModule), OAuth, and HttpService

@NgModule({
  imports: [HttpModule]
  providers: [OAuth, HttpService],
  ...
})
Sign up to request clarification or add additional context in comments.

3 Comments

I should mention that this is an ionic2 application. I don't get access to NgModule in an ionic2 app (just a root component), so I have my component providers look like this providers: [OAuth, HttpService, HTTP_PROVIDERS] but it's still null
That would be equivalent. I don't see why it wouldn't work. There were issues with DI and classes extending other classes. I don't know if they are fixed. As workaround you could embed Http instead of extending it. You would need to repeat the get, post, ... methods in your class and forward them to this.http. To check if it is related to extending other classes you could just remove extends Http and super(...) and see if all parameters are injected this way.
This works. Instead of extending, I injected both my Http, and OAuth service, and both work as expected. Either they have bugs to work out, or perhaps my providers are not setup correctly. Either way, I can continue onward. Thanks

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.