angularng2-bootstrap

ng2-bootstrap - model - Open from the component


I would like to open the dialog from the component:

    <!-- Large modal -->
<button class="btn btn-primary" (click)="lgModal.show()">Large modal</button>

<div bsModal #lgModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" (click)="lgModal.hide()" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Large modal</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
    </div>
  </div>
</div>

The lgModal.show() which is bsModel opens the dialog, how do i open the dialog from the component :

    import {Component} from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';

// todo: change to ng2-bootstrap
import {MODAL_DIRECTVES, BS_VIEW_PROVIDERS} from '../../../ng2-bootstrap';
// webpack html imports
let template = require('./modal-demo.html');

@Component({
  selector: 'modal-demo',
  directives: [MODAL_DIRECTVES, CORE_DIRECTIVES],
  viewProviders:[BS_VIEW_PROVIDERS],
  template: template
})
export class ModalDemoComponent {

}

Can't I do something like lgModal.show() from the component?


Solution

  • Add this in the Component's class implementation:

    @ViewChild('lgModal') bgModel;

    Then you can reference it in the class with this.bgModel.

    Good luck