【问题标题】:Resuming a function after modal is dismissed在模态被解除后恢复功能
【发布时间】:2019-10-16 05:14:47
【问题描述】:

我不确定这是否可行,所以会尽量解释清楚。我只在 Ionic 1 中体验过之后就跳到 Ionic 4,并且有很多成长的烦恼。在我的 Ionic 1 应用程序中,我有很多功能,其中包括 ionic 弹出窗口,并且弹出窗口将是完成功能不可或缺的一部分。

也就是说,我正在使用类似于此答案的代码:https://stackoverflow.com/a/55041136/5306408

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

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

基本上:

页面:

... 

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

...

一些服务:

...

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),以通过关闭同一个模态提供的数据来解析。

【问题讨论】:

    标签: angular ionic-framework ionic4


    【解决方案1】:

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

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

    this.loginModalActive = true; 添加到openModal,然后

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

    到 thenable 包含的modal.onDidDismiss() 调用。

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

    waitForLoginModuleToClose() {
      return new Promise(resolve => {
        const delay = 100;
    
        interval(delay).subscribe(() => {
          if (!this.loginModalActive) {
            resolve(this.dataReturned);
          }
        });
      });
    }
    

    在页面中,如果someService 调用被拒绝,则让pageFunction() 调用自身。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2013-12-25
      相关资源
      最近更新 更多