取消模态后恢复功能

时间:2019-10-10 19:01:25

标签: angular ionic-framework ionic4

我不确定是否可以做到这一点,因此将尽力解释它。在只有Ionic 1的经验之后,我将跳到Ionic 4,并且涉及很多成长中的烦恼。在我的Ionic 1应用程序中,我有很多功能,其中包括离子弹出窗口,该弹出窗口对于功能的完成必不可少。

即,我正在使用类似于以下答案的代码:https://stackoverflow.com/a/55041136/5306408

我的总体代码的基本工作方式是:

  • 我在页面中有一个可点击的项目。
  • 单击该页面可启动服务功能。
  • 服务功能调用URL,在理想情况下,将从该URL检索的数据返回到页面。
  • 但是,在某些情况下,将要求用户提供其他信息。理想情况下,我希望这种情况发生在模式中:一旦用户填写了正确的信息,就关闭了该模式,然后该函数将继续执行其最初的目标,即调用URL并返回从中获取的数据,并按预期将其返回到步骤1中的页面。

基本上:

页面:

... 

pageFunction() void {
    this.someService.someFunction('someURL').then(dataFromTheURL) => {
        //do something with dataFromTheURL
    })
}

...

someService:

...

async openModal(params) {
    const modal = await this.modalController.create({
        component: someLoginModalPage,
        componentProps: { ... }
    });

    modal.onDidDismiss().then((dataReturned) => {
        //need a way to pass dataReturned back to pageFunction()
    });

    return await modal.present();
}

someFunction(url): Promise<any> {
    return new Promise<any>((resolve) {
        if (condition is fine and no modal is needed) {
            this.http.get(url, {headers: someHeaderObject}).subscribe(result => {
                resolve(result)
            })
        }
        else {
            //here is the crux of the problem - I need indicatorThatModalHasBeenClosed to come from the modal being closed, not opened
            this.openModal(someParams).then((indicatorThatModalHasBeenClosed) => {

                //do something with indicatorThatModalHasBeenClosed

                this.http.get(url, {headers: someHeaderObject}).subscribe(result => {
                    resolve(result)
                })
            });
        }
    })
}

这可能与Angular模态有关吗?我很难找到它的任何示例,因为openModal的代码返回的是modal.present()的结果,而不是返回通过modal.onDidDismiss()传递的数据。最终,我想要一个函数(返回Promise)来打开模式,以使用通过关闭相同模式提供的数据来解析。

1 个答案:

答案 0 :(得分:0)

好吧,我想到了一种解决方法:如果模式打开,则拒绝someFunction而不是解决它。

然后,将属性dataReturnedloginModalActive添加到someFunction所属的服务中。

this.loginModalActive = true;添加到openModal,并且

this.dataReturned = dataReturned;
this.loginModalActive = false;

转到随后包含的modal.onDidDismiss()调用。

然后,将此功能添加到someFunction所属的服务中:

waitForLoginModuleToClose() {
  return new Promise(resolve => {
    const delay = 100;

    interval(delay).subscribe(() => {
      if (!this.loginModalActive) {
        resolve(this.dataReturned);
      }
    });
  });
}

在页面中,如果pageFunction()呼叫被拒绝,请someService对其进行呼叫。

这并不是我想要的,但是它为服务创建了一种方式来跟踪是否应该打开模式,并每隔100毫秒运行一次间隔以检查其是否打开。一旦确定它没有打开,就可以再次尝试尝试的页面功能。