347

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'
                })
            });
Lazar Ljubenović
  • 17,499
  • 8
  • 54
  • 86
daniel
  • 32,027
  • 36
  • 92
  • 151

16 Answers16

527

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:

Edric
  • 21,480
  • 12
  • 75
  • 86
Philip
  • 21,570
  • 2
  • 29
  • 31
57

>= 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.

Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
9

Since rc.5 you have to do something like

@NgModule({
    imports: [ BrowserModule],
    providers: [ HTTP_PROVIDERS ],  
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
export default class AppModule { }

platformBrowserDynamic().bootstrapModule(AppModule);
C_Ogoo
  • 5,926
  • 3
  • 19
  • 37
Adrian Ber
  • 18,774
  • 9
  • 64
  • 106
9

With the Sept 14, 2016 Angular 2.0.0 release, you are using still using HttpModule. Your main app.module.ts would look something like this:

import { HttpModule } from '@angular/http';

@NgModule({
   bootstrap: [ AppComponent ],
   declarations: [ AppComponent ],
   imports: [
      BrowserModule,
      HttpModule,
      // ...more modules...
   ],
   providers: [
      // ...providers...
   ]
})
export class AppModule {}

Then in your app.ts you can bootstrap as such:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/main/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);
Caleb
  • 2,208
  • 2
  • 13
  • 15
9

Add HttpModule to imports array in app.module.ts file before you use it.

import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [
    AppComponent,
    CarsComponent
  ],
  imports: [
    BrowserModule,
 HttpModule  
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Surendra Parchuru
  • 921
  • 1
  • 11
  • 18
6

because it was only in the comment section I repeat the answer from Eric:

I had to include HTTP_PROVIDERS

daniel
  • 32,027
  • 36
  • 92
  • 151
  • 2
    ... Plus, HTTP_PROVIDERS has been depreciated. It's now called *HttpModule*.. https://stackoverflow.com/questions/38903607/ng2-rc5-http-providers-is-deprecated – Mike Gledhill Jun 09 '17 at 09:53
5

Import HttpModule in your app.module.ts file.

import { HttpModule } from '@angular/http';
import { YourHttpTestService } from '../services/httpTestService';

Also remember to declare HttpModule under imports like below:

imports: [
    BrowserModule,
    HttpModule
  ],
Mwiza
  • 6,098
  • 3
  • 41
  • 35
poo arasan
  • 61
  • 1
  • 5
4

The best way is to change your component's decorator by adding Http in providers array as below.

@Component({
    selector: 'greetings-ac-app2',
    providers: [Http],
    templateUrl: 'app/greetings-ac2.html',
    directives: [NgFor, NgModel, NgIf, FORM_DIRECTIVES],
    pipes: []
})
Shivang Gupta
  • 2,881
  • 1
  • 22
  • 23
  • Who soever has marked it wrong, May i know what is the reason? – Shivang Gupta Sep 29 '16 at 04:45
  • 5
    I didn't downvote, but the reason is probably that you don't want a new `Http` object for each component. Better to have a single one for the app, which is accomplished by importing it at the `NgModule` level. – Ted Hopp Dec 04 '16 at 07:02
3

as of RC5 you need to import the HttpModule like so :

import { HttpModule } from '@angular/http';

then include the HttpModule in the imports array as mentioned above by Günter.

mareks
  • 774
  • 6
  • 5
3

Just include the following libraries:

import { HttpModule } from '@angular/http';
import { YourHttpTestService } from '../services/httpTestService';

and include the http class in providers section, as follows:

@Component({
  selector: '...',
  templateUrl: './test.html',
  providers: [YourHttpTestService]
Enayat
  • 3,424
  • 1
  • 30
  • 44
3

If you have this error in your tests, you should create Fake Service for all services:

For example:

import { YourService1 } from '@services/your1.service';
import { YourService2 } from '@services/your2.service';

class FakeYour1Service {
 public getSomeData():any { return null; }
}

class FakeYour2Service {
  public getSomeData():any { return null; }
}

And in beforeEach:

beforeEach(async(() => {
  TestBed.configureTestingModule({
    providers: [
      Your1Service,
      Your2Service,
      { provide: Your1Service, useClass: FakeYour1Service },
      { provide: Your2Service, useClass: FakeYour2Service }
    ]
  }).compileComponents();  // compile template and css
}));
Experimenter
  • 1,607
  • 15
  • 22
3
**

Simple soultion : Import the HttpModule and HttpClientModule on your app.module.ts

**

import { HttpClientModule } from '@angular/common/http';
import { HttpModule } from '@angular/http';



@NgModule({
 declarations: [
   AppComponent, videoComponent, tagDirective, 
 ],
 imports: [
  BrowserModule, routing, HttpClientModule, HttpModule

],
providers: [ApiServices],
bootstrap: [AppComponent]
})
export class AppModule { }
Shashwat Gupta
  • 4,435
  • 36
  • 28
  • This solution works, but HttpModule is marked as deprecated in Angular 5.2. I think some component is not upgraded, and still uses the old Http implementation. – Sobvan Mar 16 '18 at 20:09
1

I faced this issue in my code. I only put this code in my app.module.ts.

import { HttpModule } from '@angular/http';

@NgModule({
  imports:      [ BrowserModule, HttpModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
isherwood
  • 52,576
  • 15
  • 105
  • 143
Saurabh
  • 322
  • 2
  • 6
1

All you need to do is to include the following libraries in tour app.module.ts and also include it in your imports:

import { HttpModule } from '@angular/http';

@NgModule({
  imports:    [ HttpModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
fiza khan
  • 1,240
  • 12
  • 24
1

import { HttpModule } from '@angular/http'; package in your module.ts file and add it in your imports.

Alekya
  • 233
  • 5
  • 15
1

I just add both these in my app.module.ts:

"import { HttpClientModule }    from '@angular/common/http'; 

&

import { HttpModule } from '@angular/http';"

Now its works fine.... And dont forget to add in the

@NgModule => Imports:[] array