350

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ć
  • 18,976
  • 10
  • 56
  • 91
daniel
  • 34,281
  • 39
  • 104
  • 158

16 Answers16

528

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
  • 24,639
  • 13
  • 81
  • 91
Philip
  • 23,316
  • 2
  • 31
  • 31
  • 2
    In current Angular2 beta the file is called `app.ts`. – d135-1r43 Apr 20 '16 at 13:05
  • 7
    In angular-cli generated projects, the file is called `main.ts`. – filoxo May 16 '16 at 16:33
  • what if I don't have a NgModule? I'm developing an angular2 module and it does not have a NgModule but for tests I need Http provider – iRaS Oct 07 '16 at 06:03
  • @Webruster I just checked. It should still work. Can you give me the exact error code? – Philip Jun 11 '17 at 09:24
  • 2
    @PhilipMiglinci ...thanks for the valuable answer .. adding some points for the seekers that the file would be **app.module.ts** in angular 2.0 for explanation this is the core file for the project to include the modules which will use further inherited classes. – shaan gola Sep 23 '17 at 17:40
  • Don't forget to also add it to imports (under @NgModule) as shown in the answer, otherwise it doesn't work (for me). – Albert Hendriks Apr 12 '18 at 10:00
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
  • 623,577
  • 216
  • 2,003
  • 1,567
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.OG
  • 6,236
  • 3
  • 20
  • 38
Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
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,268
  • 2
  • 14
  • 16
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 { }
6

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

I had to include HTTP_PROVIDERS

daniel
  • 34,281
  • 39
  • 104
  • 158
  • 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
  • 7,780
  • 3
  • 46
  • 42
poo arasan
  • 61
  • 1
  • 6
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
  • 3,139
  • 1
  • 25
  • 24
  • 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,904
  • 1
  • 33
  • 47
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
  • 2,084
  • 1
  • 19
  • 26
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
  • 5,071
  • 41
  • 33
  • 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
  • 58,414
  • 16
  • 114
  • 157
Saurabh
  • 472
  • 6
  • 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,280
  • 13
  • 24
1

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

Alekya
  • 239
  • 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