ng2-bootstrap modal cant open modal

时间:2016-07-18 10:24:14

标签: angular bootstrap-modal

我有像这样的pizza.component.ts

import {Component,ViewContainerRef} from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common';
import { BUTTON_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';
import {MODAL_DIRECTIVES, BS_VIEW_PROVIDERS} from 'ng2-bootstrap/ng2-bootstrap';

@Component({
  selector: 'pizza-com',
  templateUrl: './app/pizza/pizza.html',
  directives: [BUTTON_DIRECTIVES,MODAL_DIRECTIVES,CORE_DIRECTIVES],
  viewProviders:[BS_VIEW_PROVIDERS],
})
export class PizzaComponent {
    public constructor(viewContainerRef:ViewContainerRef) {
    // You need this small hack in order to catch application root view container ref
    this.viewContainerRef = viewContainerRef; <<<---this doesn't seems to work dunno why
  }
}

和我的pizza.html一样

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

当我按下模态按钮时,显示错误

platform-browser.umd.js:1900 
EXCEPTION: Error: Uncaught (in promise): 
TypeError: Cannot read property 'parentInjector' of undefined

我做错了什么?

我也跟随ng2-bootstrap中的the example,但 this.viewContainerRef 似乎无法正常工作

1 个答案:

答案 0 :(得分:0)

我使用的是Angular 2 Final / ng2-bootstrap 1.1.3 / systemjs

import { Component, ViewContainerRef } from '@angular/core';
@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>',
  encapsulation: ViewEncapsulation.None
})
export class AppComponent implements OnInit {
  private viewContainerRef: ViewContainerRef;

  constructor(viewContainerRef: ViewContainerRef) {
    this.viewContainerRef = viewContainerRef;
  }
}