angular - 以角度存储可观察的数据列表以进行第一次调用并使用存储的值
问题描述
对此概念的任何帮助将不胜感激。我希望将 Angular 中第一次调用 API 的员工列表存储为可观察的。但是对于所有连续的呼叫,我想使用存储的 listOfEmployees 。我尝试了各种方法,但没有运气。
我的 Employee.ts 类
export class Employee
{
EmployeeName: string;
Department: string;
}
我的 Service.ts 类
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;
}
解决方案
推荐阅读
- python - 如何构建分层张量流数据集?
- django - Django Elastic APM Trace 日志消息丢失
- java - 如何为使用私有方法的类编写测试用例?
- anylogic - 如何从上传的数据库表中将代理注入源块中?
- java - 测试端点定义和所需参数的最简单方法是什么
- mysql - 我正在尝试将数据从 MySQL Workbench 导出到 csv,但它不断将特定列值转换为科学记数法
- javascript - 有没有办法在 React 的 componentDidMount 方法中使用事件对象?
- python - 我可以将 UDP 数据包从我的电脑发送到另一台电脑,但我无法发送 tcp 数据包
- blockchain - 获取 Avalanche C-Chain 合约 Abi 的
- keras - 如何返回从 Keras model.fit_generator 生成的样本?