【发布时间】:2018-03-27 10:52:26
【问题描述】:
我在我的 Angular 应用程序中遇到了这个问题,因为我正在缓存数据。在我使用 onCreate() 函数添加内容之后。我在成功订阅函数中调用了 getAll() 函数以获取新数据。但是,我仍然没有得到新的数据。我相信这是因为我的服务中的缓存数据。我如何知道是否有新数据或如何修复我的缓存现有数据并在有新数据时刷新数据?
服务
getAll() {
if(!this.materials) {
this.materials = this.httpClient.get<any>(this.url)
.map((response => response))
.publishReplay(1)
.refCount();
}
return this.materials;
}
getAll() ts
getAllMaterials() {
this.subscription = this.materialsService.getAll()
.subscribe(
(data:any) => {
this.materials = data.materials;
console.log(data);
},
error => {
alert("Error");
console.log(error);
});
}
onCreate() -ts
onCreateMaterial(form: NgForm){
const formData = {
sku: form.value.sku,
name: form.value.name,
supplier_id: form.value.supplier,
price: form.value.price
}
this.materialsService.addMaterial(formData)
.subscribe(
data => {
let message = data.message;
alert(message);
console.log(data);
this.modalRef.close();
this.getAllMaterials();
},
error => {
alert("Error Adding");
console.log(error);
});
}
【问题讨论】:
标签: angular rxjs angular-services angular-forms angular-cache