I am getting the EXCEPTION: No provider for Http!
in my Angular app. 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' }) });
Import the HttpModule
import { HttpModule } from '@angular/http'; @NgModule({ imports: [ BrowserModule, HttpModule ], providers: [], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export default class AppModule { } platformBrowserDynamic().bootstrapModule(AppModule);
Ideally, you split up this code in two separate files. For further information read:
>= Angular 4.3
for the introduced HttpClientModule
import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [ BrowserModule, FormsModule, // if used HttpClientModule, JsonpModule // if used ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
Angular2 >= RC.5
Import HttpModule
to the module where you use it (here for example the AppModule
:
import { HttpModule } from '@angular/http'; @NgModule({ imports: [ BrowserModule, FormsModule, // if used HttpModule, JsonpModule // if used ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
Importing the HttpModule
is quite similar to adding HTTP_PROVIDERS
in previous version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With