【问题标题】:Close a ionic modal in a service关闭服务中的离子模式
【发布时间】:2017-07-05 20:32:05
【问题描述】:

我正在使用 Ionic 3 (Angular 4),我想在服务中关闭一个 ionic modal,这可能吗?

Screen of DataService.ts

我想在我的 DataService.ts 的另一个函数中调用 close() 函数来关闭我的模态 deleteconfirm.ts

【问题讨论】:

  • 我会说不要将视图代码放在服务中,你只是在问头疼。而是让服务返回一个 Promise 并在 Promise 中调用 close。

标签: angular typescript ionic-framework


【解决方案1】:

从您的代码看来,在我看来,您的服务必须了解该视图。

您可以做的是在打开模式时将模式的实例添加到您的服务中,以便您可以引用该实例以便能够在服务中关闭它。

示例(只是一个虚构的示例,因为您示例中的代码太简短了):

// Example component
export class MyComponent implements OnInit {
    modal: Modal;

    constructor(private modalService: ModalService) {}

    ngOnInit() {
        this.modalService.modal = this.modal;
    }

    close() {
        this.modalService.close();
    }
}

// Example service
export class ModalService {
    _modal: Modal;

    set modal(modal: Modal) {
        this._modal = modal;
    }

    close() {
        if (this._modal) {
            this._modal.close();
            this._modal = null;
        }
    }
}

这种方法具有可移植性,因为您可以通过注入 ModalService 来关闭您希望的任何组件的模式。

【讨论】:

  • 感谢您的回答,感谢我的 HTML 中的这段代码:(click)="dataService.deleteItem(cle);close()",太棒了,我不知道是可能的^^
  • 没问题!!是的,在大多数情况下,Angular 让你可以随心所欲地表达——我想这可能是好的也可能是坏的。
猜你喜欢
  • 2022-07-04
  • 2017-06-05
  • 1970-01-01
  • 2018-12-08
  • 2018-01-18
  • 1970-01-01
  • 2018-04-27
  • 2014-10-03
  • 1970-01-01
相关资源
最近更新 更多