Search code examples
angularangular2-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. 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>

Solution

  • It seems you are using the declarative dialog form 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

    In your code you have to grab the reference (#loginDialog) for your loginDialog from the view template in your ts file (component file):

    @ViewChild('loginDialog') loginDialog: MdlDialogComponent;
    

    in you loginGoogle methode you can now do:

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