简体   繁体   English

如何关闭组件文件中的 mdl 对话框

[英]How to close mdl dialog in component file

Hi i am new to Angular 2. I am using angular2 mdl dialog and i want to close dialog when login is success.嗨,我是 Angular 2 的新手。我正在使用 angular2 mdl 对话框,我想在登录成功时关闭对话框。 But i dont know how to close dialog.Can any one help me please但我不知道如何关闭对话框。请任何人帮助我

<mdl-dialog #loginDialog [mdl-dialog-config]="{
          clickOutsideToClose: true,
          isModal:true,
          openFrom: loginButton,
          enterTransitionDuration: 400,
          leaveTransitionDuration: 400}">
    <h3 class="mdl-dialog__title">Login</h3>
    <div class="mdl-dialog__content">
    <form name='loginForm' #loginForm="ngForm">
        <md-input-container>
            <md-icon class="inputIcon">perm_identity</md-icon>
            <input type="text" mdInput name="Email" [(ngModel)]="userEmail" placeholder="Email" #Email="ngModel" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" required>
        </md-input-container>
        <div [hidden]="Email.valid || Email.pristine" class="error">
                    Email is invalid!
                </div>
        <md-input-container>
            <md-icon class="inputIcon">lock</md-icon>
            <input type="password" mdInput name="Password" [(ngModel)]="password" placeholder="Password" required>
        </md-input-container>
        <button class="btn loginBlue" [disabled]="!loginForm.form.valid" (click)="loginEmail(); false">Login</button>
        <p style="text-align: center;font-size: 14px;font-family: 'Open Sans';color: #909090;margin-top: 20px;margin-bottom: 15px;">or</p>
        <button class="loginRed" (click)="loginGoogle(); false"><img src="/images/google_icon.png" class="google">Login using google</button>
    </form>
    </div>
</mdl-dialog>

It seems you are using the declarative dialog form http://mseemann.io/angular2-mdl/dialogs-declarative .您似乎正在使用声明性对话框表单http://mseemann.io/angular2-mdl/dialogs-declarative The sources for this demo can be found here: https://github.com/mseemann/angular2-mdl/blob/master/src/demo-app/app/dialog-declarative/dialog-declarative.component.ts这个演示的源可以在这里找到: https : //github.com/mseemann/angular2-mdl/blob/master/src/demo-app/app/dialog-declarative/dialog-declarative.component.ts

In your code you have to grab the reference ( #loginDialog ) for your loginDialog from the view template in your ts file (component file):在您的代码中,您必须从 ts 文件(组件文件)中的视图模板中获取#loginDialog的引用( #loginDialog ):

@ViewChild('loginDialog') loginDialog: MdlDialogComponent;

in you loginGoogle methode you can now do:在您 loginGoogle 方法中,您现在可以执行以下操作:

public loginDialog() {
   // do the login and if you are done:
   this.loginDialog.close();
}

In your parent component add the below line to constructor在您的父组件中,将以下行添加到构造函数

constructor(public dialog: MdDialog) {}

On click of Login in modal dialog call the parent component's close method as below在模态对话框中单击 Login 调用父组件的 close 方法,如下所示

<button (click)="loginDialog.close()"> Login </button>

Update :更新 :

The code to open the dialog打开对话框的代码

 openDialog() {
    let dialogRef = this.dialog.open(ParentDialog);
    dialogRef.afterClosed().subscribe(result => {
      //your action
    });
  }

The code to close the dialog关闭对话框的代码

<button class="btn loginBlue" 
   (click)="dialogRef.close()">Login</button>

LIVE DEMO现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM