zoidbergi zoidbergi - 5 months ago 646
AngularJS Question

Angular 2 No provider for Http

I am getting the

EXCEPTION: No provider for Http!
in my Angular 2. What am I doing wrong?

import {Http, Headers} from 'angular2/http';
import {Injectable} from 'angular2/core'


@Component({
selector: 'greetings-ac-app2',
providers: [],
templateUrl: 'app/greetings-ac2.html',
directives: [NgFor, NgModel, NgIf, FORM_DIRECTIVES],
pipes: []
})
export class GreetingsAcApp2 {
private str:any;

constructor(http: Http) {

this.str = {str:'test'};

http.post('http://localhost:18937/account/registeruiduser/',
JSON.stringify(this.str),
{
headers: new Headers({
'Content-Type': 'application/json'
})
});

Answer

here is the code example for @serj-sagan

HTTP_PROVIDERS have to be included to your boot.ts, app.ts or main.ts. (depends on your configuration)

the content of the boot.ts file will look like:

import {bootstrap}    from 'angular2/platform/browser'
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppComponent} from './app.component'

bootstrap(AppComponent, [HTTP_PROVIDERS]);

Note: On newer versions of angular following works:

import {bootstrap}    from '@angular/platform/browser'
import {HTTP_PROVIDERS} from '@angular/http';
import {AppComponent} from './app.component'

bootstrap(AppComponent, [HTTP_PROVIDERS]);