I am facing above mentioned error when I am using Http
inside the constructor of my service.
Employee service code
import { Component } from '@angular/core'
import { Injectable } from '@angular/core'
import { HttpModule } from '@angular/http';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/map';
@Injectable()
export class employeService {
constructor(public http: Http){ }
getEmployeename(): Observable<string> {
return this.http.get('http://localhost:53656/api/values/Getdata').map((resp: Response) =>
resp.json()
);
}
}
I am calling the service directly (not from app.module.ts
file) inside employee component as
Employee Component code
import { Component, OnInit } from '@angular/core';
import { employeService } from './Service/service';
@Component({
templateUrl: './app.employee.html',
providers: [employeService]
})
export class EmployeeComponent implements OnInit {
empName: string;
constructor(private empService: employeService) { }
ngOnInit() {
this.empService.getEmployeename().subscribe(empName =>
this.empName = empName
);
}
}
And I have this error message in Console :
core.js:1598 ERROR Error: Uncaught (in promise): Error:
StaticInjectorError(AppModule)[employeService -> Http]:
StaticInjectorError(Platform: core)[employeService -> Http]:
NullInjectorError: No provider for Http!
Error: StaticInjectorError(AppModule)[employeService -> Http]:
StaticInjectorError(Platform: core)[employeService -> Http]:
NullInjectorError: No provider for Http!
at
You need to import HTTPModule into your main module.
import { HttpModule } from '@angular/http';
@NgModule({
declarations: [ ],
imports: [
......
HttpModule
]
....
As per latest versions you should use HttpClientModule
instead of HttpModule
Import like this
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [ ],
imports: [
......
HttpClientModule
]
....
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