【发布时间】:2017-09-11 15:30:52
【问题描述】:
大家好, 我有一个疯狂的想法,将服务用作缓存存储。所以我在服务中定义了一些变量。数据在服务中可以正常更新,但更改不会反映在视图上。视图已经被填充并且服务层随后运行。我不确定,即使在服务中存储数据是个好主意?请有人解释一下。 这是我的服务代码:
categories : SelectItem[]=[];
constructor(private utilityService:UtilityService) {
}
//populate all drop downs array with initial data from db
populateAll() {
//get categories
this.getCategories();
//there are lot more calls here
}
private getCategories() {
if(this.categories.length==0){
this.utilityService.getAllCategories().subscribe(cat=>{
this.categories =
this.utilityService.getSelectItemPublished(cat,"Category");
})
}
}
这就是我在组件中的称呼
constructor(
private cache:CacheStoreService,
) { }
ngAfterViewInit()
{
this.categories=this.cache.categories;
this.depts=this.cache.depts;
this.focuses=this.cache.depts;
this.statuses=this.cache.statuses;
this.phases=this.cache.statuses;
this.visibilities=this.cache.visibilities;
}
ngOnInit() {
this.cache.populateAll();
感谢您的帮助。还有一个问题:当在 db-update 缓存中添加新数据时,如何更新服务变量。 **我知道,我可以返回 observable 并在组件中订阅它。我正在寻找另一种方式。不确定,如果可以吗? **
谢谢
【问题讨论】:
-
很难用给定的代码来判断,但是在组件的订阅块中,您的下一个回调看起来像是在调用另一个函数。你在那里正确地打盹你的结果吗?如果该函数返回另一个 observable,您可以在模板中使用异步管道。至于您的第二个问题,我相信您需要某种实时数据库解决方案。
-
我知道,我可以让返回 observable 并在组件中订阅它。我正在寻找另一种方式。不确定,如果可以吗?
标签: angular