如何在 Ionic 2 中等待值
How to wait value in Ionic 2
我正在尝试等待执行后可能会更改的值 "this.bookmarList.take(1).subscribe(..."
我的问题是,当我 运行 应用程序时,它会在执行我的订阅之前执行第二个 if 语句 "if(found==false)"。
这是我的代码,您可能会发现一些错误或需要添加一些补充来解决我的问题。
let loader = this.loadingCtrl.create({
content: "",
});
let found=false;
loader.present().then(()=>{
this.bookmarkList.take(1).subscribe(data=>{
data.forEach(b=>{
if(b.nID==this.appService.id && b.url==url){
let alert = this.alertCtrl.create({
title: 'Already Bookmarked',
subTitle: 'This note image already bookmarked',
buttons: ['OK']
});
alert.present();
found=true;
}
})
})
}).then(()=>{
if(found==false){
this.afd.list('/bookmark/').push({
uemail: this.userService.email,
nID: this.appService.id,
url: url,
date: dateX
});
let alert = this.alertCtrl.create({
title: 'Bookmarked Successfully',
subTitle: 'This note image has been bookmarked successfully',
buttons: ['OK']
});
alert.present();
}
}).then(_=>{
loader.dismiss();
})
我能找到解决方案
let loader = this.loadingCtrl.create({
content: "",
});
let found=false;
loader.present().then(_=>{
this.bookmarkList.take(1).subscribe(data=>{
data.forEach(b=>{
if(b.nID==this.appService.id && b.url==url){
let alert = this.alertCtrl.create({
title: 'Already Bookmarked',
subTitle: 'This note image already bookmarked',
buttons: ['OK']
});
alert.present();
found=true;
}
})
if(found==false){
this.afd.list('/bookmark/').push({
uemail: this.userService.email,
nID: this.appService.id,
url: url,
date: dateX
});
let alert = this.alertCtrl.create({
title: 'Bookmarked Successfully',
subTitle: 'This note image has been bookmarked successfully',
buttons: ['OK']
});
alert.present();
}
//return
})
}).then(_=>{
loader.dismiss();
})
我正在尝试等待执行后可能会更改的值 "this.bookmarList.take(1).subscribe(..."
我的问题是,当我 运行 应用程序时,它会在执行我的订阅之前执行第二个 if 语句 "if(found==false)"。
这是我的代码,您可能会发现一些错误或需要添加一些补充来解决我的问题。
let loader = this.loadingCtrl.create({
content: "",
});
let found=false;
loader.present().then(()=>{
this.bookmarkList.take(1).subscribe(data=>{
data.forEach(b=>{
if(b.nID==this.appService.id && b.url==url){
let alert = this.alertCtrl.create({
title: 'Already Bookmarked',
subTitle: 'This note image already bookmarked',
buttons: ['OK']
});
alert.present();
found=true;
}
})
})
}).then(()=>{
if(found==false){
this.afd.list('/bookmark/').push({
uemail: this.userService.email,
nID: this.appService.id,
url: url,
date: dateX
});
let alert = this.alertCtrl.create({
title: 'Bookmarked Successfully',
subTitle: 'This note image has been bookmarked successfully',
buttons: ['OK']
});
alert.present();
}
}).then(_=>{
loader.dismiss();
})
我能找到解决方案
let loader = this.loadingCtrl.create({
content: "",
});
let found=false;
loader.present().then(_=>{
this.bookmarkList.take(1).subscribe(data=>{
data.forEach(b=>{
if(b.nID==this.appService.id && b.url==url){
let alert = this.alertCtrl.create({
title: 'Already Bookmarked',
subTitle: 'This note image already bookmarked',
buttons: ['OK']
});
alert.present();
found=true;
}
})
if(found==false){
this.afd.list('/bookmark/').push({
uemail: this.userService.email,
nID: this.appService.id,
url: url,
date: dateX
});
let alert = this.alertCtrl.create({
title: 'Bookmarked Successfully',
subTitle: 'This note image has been bookmarked successfully',
buttons: ['OK']
});
alert.present();
}
//return
})
}).then(_=>{
loader.dismiss();
})