ng2-bootstap - model - 从组件

时间:2016-06-02 21:00:34

标签: angular ng2-bootstrap

我想从组件中打开对话框:

    <!-- 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>

lgModal.show()是bsModel打开对话框,如何从组件中打开对话框:

    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 {

}

我可以从组件中执行类似lgModal.show()的操作吗?

提前致谢

1 个答案:

答案 0 :(得分:4)

在Component的类实现中添加它:

@ViewChild('lgModal') bgModel;

然后,您可以使用this.bgModel在课程中引用它。

祝你好运