我已经将Ionic 2提示符放在了一个提供商(服务)中,如下所示。
showPromptOk(title, message, callback) {
let prompt = this.alertCtrl.create({
title: title || 'Alert!',
message: message,
buttons: [
{
text: 'Ok',
handler: data => {
if(callback) callback(data);
}
}
]
});
prompt.present();
}我这样叫它..
updateAccount() {
let self = this;
this.sk_account.update(this.account)
.subscribe((res) => {
this.sk_utils.showPromptOk('Success!', 'Your account has been updated!', (res) => {
self.edit_account = false;
self.edit_school = false;
});
}
)
}问题是," this“或"self”在这种情况下不再有效,它变成了提供者(服务)的上下文。如何将处理程序设置为将" scope“传递给原始调用者,以便当我设置值时,它们实际上是在原始组件作用域中设置它们,而不是在提供者作用域中?
更新1:
添加回调(Data)后,我注意到视图模型不再更新。您可以在控制台中看到这些值现在被设置为false,但是视图上的{{edit_account}}插值没有得到更新。请注意,该变量在视图上仍为"true“。有什么想法吗?

请注意,在控制台中,数据不是false,但视图不会更新。

发布于 2018-12-09 19:12:05
尝试使用Subject:showPromptOk获取subject,处理程序调用Account组件中的'next方法‘,添加subject及其订阅,更新逻辑必须在订阅中
Ps:主题必须是静态的
static AccountMustBeUpdated: Subject<any> = new Subject<any>();
ionViewDidLoad(){
AccoutsPage.AccountMustBeUpdated.subscribe(
() => { /* Your update logic here*/} );别忘了取消订阅该主题以避免麻烦
https://stackoverflow.com/questions/43707893
复制相似问题