模态显示不正确 angular 4

Modal doesn't appear correctly angular 4

我正在使用 ng-bootstrap Here try to do a modal sample ,but when i click on open modal it doesn't appear although it exists inside the DOM.

我的angular核心版本:4.0.0,@ng-bootstrap/ng-bootstrap:1.0.0-alpha.29,bootstrap:3.3.7

测试-modal.component.html

<div class="modal-header">
      <h4 class="modal-title">Hi there!</h4>
      <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <p>Hello, {{name}}!</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Close</button>
    </div>

测试-modal.component.ts

import {Component,Input} from '@angular/core';

import {NgbModal,NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-test-modal',
  templateUrl: './test-modal.component.html',
  styleUrls: ['./test-modal.component.css']
})
export class TestModalComponent  {

@Input() name;

  constructor(public activeModal: NgbActiveModal) {}
}

app.component.ts中的open函数与构造函数

 constructor(private modalService: NgbModal) {}
  open() {
    const modalRef = this.modalService.open(TestModalComponent);
  }

里面的按钮app.component.html

<button class="btn btn-lg btn-outline-primary" (click)="open()">Launch demo modal</button>

我认为问题可能出在您的 bootstrap 版本 bootstrap :3.3.7,当我进入主页时它说它适用于 Bootstrap 4(它还说 bootstrap 4 就开始了)。

并在 Angular 4 + Bootstrap 4 项目中进行测试,我让它工作了

package.json

"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.29",
"bootstrap": "^4.0.0-alpha.6",

在我的模块中

imports: [
  NgbModule.forRoot(), ...
],
entryComponents: [ModalTemplateComponent],

在组件中:

modal: NgbModalRef;

constructor(private modalService: NgbModal) { }  

async open() {
  this.modal = this.modalService.open(ModalTemplateComponent);

  const result = await this.modal.result;

  console.log(result);
}