2

I am new to Angular and Http service. I created a employee-list comp and a service with the name employee.service.ts which makes an Http call. I also, mapped the observable correctly,

I also create a json file in assets with the name employee.json in which store my data. There's no any compilation errors. but It fails during runtime. I'm getting following error.

NullInjectorError: R3InjectorError(AppModule)[EmployeeService -> EmployeeService -> EmployeeService]:

employee-list.component.html

<h2>Employee List</h2>
    <ul *ngFor="let employee of employees">
      <li>{{employee.name}}</li>
    </ul>

employee.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IEmployee } from '../employee';
import { Observable } from 'rxjs';

@Injectable() export class EmployeeService{

    private _url: string ="/assets/data/employee.json";

    constructor(private http:HttpClient) {}

    getEmployees(): Observable<IEmployee[]>{
        return this.http.get<IEmployee[]>(this._url);
    }
}

employee-list.component.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IEmployee } from '../employee';
import { Observable } from 'rxjs';

@Injectable() export class EmployeeService{

    private _url: string ="/assets/data/employee.json";

    constructor(private http:HttpClient) {}

    getEmployees(): Observable<IEmployee[]>{
        return this.http.get<IEmployee[]>(this._url);
    }
}
2
  • I think you have two classes with same name EmployeeService Commented Apr 27, 2020 at 5:41
  • @Azad No, there's only one class with name "EmployeeService" Commented Apr 27, 2020 at 5:55

1 Answer 1

2

error is pretty clear. you should have to provide employeeService in module.ts

import employeeService 

and add "EmployeeService" to the providers array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.