if you followed the first tutorial on how to create a service, this steps is a followup by fetching JSON data from a remote server.
open app.module.ts
ADD: import { httpClientModule } from '@angluar/common/http';
ADD in imports:[httpClientModule]
Create a new file called employee.ts and ADD the following code:
export interface IEmployee{
id: number,
name: string,
age: number
}
employee.service.ts
ADD THESE IMPORTS:import { HttpClient } from '@angular/common/http';
import { IEmployee} from './employee';
ADD String variable for the remote server which contains the json dataprivate url: string = "https://randomuser.me/api/?results=5"
ADD IN Constructorconstructor(private http:HttpClient ){}
create a method inside the export class EmployeeService
getEmployees(): Observable<IEmployee[]>{
return this.http.get<IEmployee[]>(this.url);
}
employee-list.component.ts
ngOnInit(){
this._employeeService.getEmployees()
.subscribe(data => this.employees = data);
}
Open employee.component.html
<pre>{{employees |json}}</pre>
This youtube show how to perform these steps in more details. I've tried both methods and they worked for me as of May 2020