模态没有正确出现角度4

时间:2017-07-26 22:57:05

标签: javascript angular ng-bootstrap

我正在使用ng-bootstrap Here尝试做一个模态样本,但是当我点击打开模态时,虽然它存在于DOM中,但它不会出现。 enter image description here

my angular core version: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中打开函数
 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>

1 个答案:

答案 0 :(得分:3)

我认为问题可能是你的引导版本bootstrap :3.3.7,当我到达主页时它表示它是针对Bootstrap 4的(它还说明了启动时的bootstrap 4)。

在Angular 4 + Bootstrap 4项目中进行测试我得到了它的工作

example

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);
}