【发布时间】:2017-03-26 20:35:08
【问题描述】:
我将 AngularFire2 与 Ionic2 应用程序一起使用。我正在尝试从数据库加载元素列表并在加载时显示微调器。
当列表为空时,我遇到了一个问题,我不知道如何通知控制器停止微调器,因为在新添加之前不会有任何数据。我可能没有正确的结构。
所以这是我服务的摘录:
this.userTourList = this.af.database.list('/userProfiles/' + this.userId + '/tours/');
this.userTourList.subscribe((response) => {
response.map((tourData) => {
//Create and fill a TourSnapshot component
let tourSnap = new TourSnapshot(tourData.name, tourData.$key);
tourSnap.description = tourData.description;
//[..]
this.tours.push(tourSnap);
//Return an array through my own observable
return this.toursObservable.next(this.tours);
});
}
这是我的控制器:
let loader = this.loadingCtrl.create({
content: "Loading tours..."
});
loader.present().then(() => {
//The list will be updated automatically
tourData.getExpertTourList().subscribe(tourList => {
this.tourList = tourList;
loader.dismiss().catch(() => {});
});
});
如果列表为空,我的加载器将永远不会被解雇(直到完成新的添加)。我考虑过在我的服务中对 observable 使用 isEmpty() 方法,如下所示:
this.userTourList.isEmpty().subscribe(isEmpty => {
//Force a return if the "tours" node is empty to indicate to the controller
//that nothing else will be returned before a new add
if (isEmpty) {
return this.userToursObservable.next(this.tours);
}
});
但我总是得到一个错误的 isEmpty 值。如何在不额外调用数据库的情况下检查我的列表是否为空? (如果可能的话)。
谢谢
【问题讨论】:
-
您需要包含更多代码来显示您在服务中所做的事情。如果没有数据,AngularFire2
listobservables 将发出一个空数组,因此这完全取决于您的服务中发生了什么。 -
感谢您的回答。我更新了问题。实际上 AngularFire2 列表返回一个空数组,我必须在调用我的 map() 方法之前检查它并直接返回一个空数组。我会把它作为解决方案发布。再次感谢您。
标签: angular firebase ionic2 angularfire2