I have Created an example for getting data from API "https://reqres.in/api/users". I have used services and Observable in my Example . I am Getting an Error"Error: Error trying to diff '[object Object]'.Only arrays and iterables are allowed"
Here is my Code.
employee.ts
export interface IEmployee{
id : number,
email : string,
first_name : string
}
employees.service.ts
import { IEmployee } from './employee';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class EmployeesService {
private _url : string = "https://reqres.in/api/users";
constructor( private http : HttpClient ) { }
getEmployees(): Observable<IEmployee[]>{
return this.http.get<IEmployee[]>(this._url);
}
}
employee-list.component.ts
import { EmployeesService } from './../employees.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'employee-list',
templateUrl: './employee-list.component.html',
styleUrls: ['./employee-list.component.css']
})
export class EmployeeListComponent implements OnInit {
public employees = [];
constructor(private employeesService : EmployeesService) { }
ngOnInit() {
this.employeesService.getEmployees()
.subscribe(data => this.employees = data);
}
}
employee-list.component.ts
<h2>Employee List</h2>
<ul *ngFor = "let employee of employees">
<li>{{employee.email}}</li>
</ul>