0

Context:

I have a class that is going to form POST requests for my REST Api, lets call it requester. Every ajax call made in my app will be sent through this. Thus I don't want to provide Http via bootstrap(foo, [HTTP_PROVIDERS]); That would make it global and I need it to be injected to requester

Code:

import {Http} from '@angular2/http';
@Injectable()
@Component({providers: [Http]})
export class requester {
    constructor(private http:Http){}
    ...
}

Error:

ORIGINAL EXCEPTION: No provider for Http! (requestor -> Http)

Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45
ZaksBack
  • 189
  • 3
  • 10

3 Answers3

1

try this one

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

    @Component({
          ....
           providers: [HTTP_PROVIDERS]
            ....})
    export class requester {
        constructor(private http:Http){}
        ...
    }

Assuming missing providers for your request

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
0

solve it like this

import { Http, Response } from '@angular/http';
export class LoginService {

  constructor(private http: Http) {

  }
}

or use it like this

public constructor(@Inject(Http)  private http: Http) {}
rashfmnb
  • 9,959
  • 4
  • 33
  • 44
0

Assuming requester is a service.

Each Component which uses your service requester have to reference HTTP_PROVIDERS:

import {HTTP_PROVIDERS} from '@angular2/http';

@Component({
  ...
  providers: [HTTP_PROVIDERS, requester],
  ...
})

export class MyComponent{
  constructor(private _req: requester) {
  }
}
Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45
  • Why cant the *requester* be the only one that requires the HTTP_PROVIDER? – ZaksBack Jul 27 '16 at 19:16
  • I'm guessing it's somehow related to the way how annotation is converted into metadata for `Injectable` enities. Check more information http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html – Andrei Zhytkevich Jul 27 '16 at 19:24