【问题标题】:Calling parent component function from angular 4 material dialog从 Angular 4 材质对话框调用父组件函数
【发布时间】:2018-05-26 03:46:32
【问题描述】:

我的父组件中有一个工作函数(比如 functionA)。 但是,我想通过添加基本上包含“是”和“否”按钮的 Angular Material 对话框来使它变得更好。单击“否”将关闭工作正常的对话框。现在,如果我单击“是”,我希望它调用“functionA”。 如何使用 Angular 4 做到这一点?

【问题讨论】:

    标签: angular dialog angular-material


    【解决方案1】:

    这一切都在文档中,您可能还没有阅读:

    let dialogRef = this.dialog.open(MyDialogComponent);
    
    dialogRef.afterClosed().subscribe(result => {
      this.functionA();
    });
    

    在您的模式中,按钮应该是

    <button mat-button [mat-dialog-close]="true">Yes</button>
    

    这将在订阅中返回 true。

    【讨论】:

    • 我尝试将“否”设置为 false,但它也触发了函数 A。知道为什么会这样吗?
    • 因为你的通话没有条件?只需在通话前添加if (result) ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多