angularngx-modal

How to close the modal dialog programmatically in ngx-modal


I am trying to create a login dialog box which should close on a successful login

I have created the login component and here is the html for it

    <modal #myModal submitButtonLabel="Login" (onSubmit)="onLogin()">
  <modal-header>
    <h1>Login</h1>
  </modal-header>
  <modal-content>
    <form>
      <div class="form-group">
        <label for="username">Username</label>
        <input type="text" class="form-control" id="username" placeholder="enter username"
               [(ngModel)]="user.username" name="username" required>
      </div>
      <div class="form-group">
        <label for="password">Password</label>
        <input type="password" class="form-control" id="password" placeholder="enter password"
               [(ngModel)]="user.password" name="password" required>
      </div>
      <hr/>
      <div class="form-group">
        <div class="row">
          <div class="col-md-1">
            <i class="fa fa-facebook-official"></i>
          </div>
          <div class="col-md-1">
            <i class="fa fa-google"></i>
          </div>
          <div class="col-md-1">
            <i class="fa fa-cc-visa"></i>
          </div>
        </div>
      </div>
    </form>
  </modal-content>
  <modal-footer>
    <button class="btn btn-primary" (click)="myModal.close()">close</button>
  </modal-footer>
</modal>

How do I get a reference to the modal defined in the html so that I can close the modal dialog ?


Solution

  • The way you got the reference with #myModal and the click handler myModal.close() should work just fine.

    Do you want to do this in the controller? In that case, you can use the @ViewChild('myModal') decorator.