2017-08-24 24 views
1

我有一个md对话框组件 - DialogComponent - 我从兄弟组件打开 - SiblingComponent - 我想通过一些动作关闭它在dialog.component.ts在它的component.ts中关闭md对话框

DialogComponent基本上是一个提交按钮的形式,提交表格带我到dialog.component.ts功能,我正在做一些验证和发送数据到服务。

验证通过后,并发送数据,我想作出一些超时,然后自动关闭拨号窗口,但我不知道如何在dialog.component.ts

回答

1

您可以注入MdDialogRef到对话框组件类,并用它来关闭它举例:

export class DialogComponent { 

    constructor(private dialogRef: MdDialogRef<DialogComponent >) { } 


    someAction() { 

     // do your thing here 

     this.dialogRef.close(); // <- this closes the dialog. 
     // You can also wrap it in setTimeout() if you want 
    } 
} 
+0

他试图从不同的组件达到他dialogRef – Carsten

+0

actualy没有,我试图从DialogComponent :) – esquarial

+0

@Carsten达到dialogRef,我会劝你再阅读;) –

-1

运行像md-dialog-close你需要去的组件您参考想通过父母中的@ViewChild(MyComponent) myComponent;访问。

从1兄弟姐妹,你需要通过使用@Output() event = new EventEmitter();发送一个事件给父母,然后在父母你可以通过使用它的引用访问其他兄弟。你不需要@Output(),你也可以在父组件@ViewChild(SiblingOneComponent)& @ViewChild(SiblingTwoComponent)中创建两个引用,并在子组件中设置一个变量:parentComponent:ParentComponent。它(父)利用SiblingOneComponent.parentComponent =这一点;

+1

我不认为这是。 OP在询问什么,他只想从对话框组件类中关闭对话框实例。 –