Any help in this concept will be deeply appreciated. I am looking to store the list of employees from the first call of API in Angular as observable. But for all the consecutive calls I want to use the stored listOfEmployees . I tried various approaches with no luck.
My Employee.ts class
export class Employee
{
EmployeeName: string;
Department: string;
}
My Service.ts class
getListOfEmployeeFromAPI(data: any): Observable<Employee[]> {
if(!!this.listOfEmployees) //Check if Employee list if stored earlier by the first call. If the list is empty then call API else get the Employee list from the variable listOfEmployees
{
let _url = this.myLocalURL+'GetEmployeeList';
return this.http.post<Employee[]>(_url,data).
pipe(
tap(result =>
{
this.storeEmployeeRecords(result);
}
),
catchError(err=>{return this.errorHandler(err)})
);
}
else
{
//How to return observable here.
}
}
listOfEmployees:Employee[];
//Here I am storing the list of employess in the local variable.
storeEmployeeRecords(ListOfEmployees:Employee[])
{
this.listOfEmployees = ListOfEmployees;
}