Angular2 ng-bootstrap 模态关闭组件

Angular2 ng-bootstrap Modal close in component

当输入正常时,我将通过保存关闭组件中的 bootstrap 模态。 我正在使用 angular v2.4.1 和 ng-bootstrap 1.0.0-alpha.19

我已尝试 解决方案,但出现以下错误:

No provider for ViewContainerRef

我试图将 ViewContainerRef 添加到我的 module.ts 但我收到以下错误:

Type 'typeof ViewContainerRef' is not assignable to type 'Provider'

这是我组件中的代码:

import { Component } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

@Component({
    selector: '[add-name]',
    templateUrl: '../add-name.html'
})

export class MyComponent {   
   public errorMessage: string = '';

   constructor(public modalActive: NgbActiveModal) {}

   public saveNewStuff(name: string) {
        this.errorMessage = '';

        if (name === '' || name === undefined) {
            this.errorMessage = 'some message';
        } else if (this.errorMessage === '') {
            this.modalActive.close();
        }
    }
}

我也试过像

一样使用NgbModalRef
  constructor(public modalRef: NgbModalRef) {}

 public closeModal() {
    this.modalRef.close();
    this.modalRef.dismiss();
}

但这根本不起作用。我什至没有收到错误消息。

在 angular 2 中使用 bootstrap 模态弹出窗口 :

在Html中:只需复制并粘贴bootstrap模态弹出代码。

在 Ts 文件中:

import {Component} from 'angular2/core';

@Component({
    selector: 'mymodal',
    templateUrl: 'html page url'
})
export class ModalComp {
}

然后您可以通过导入 TS 文件在任何 html 视图中使用此组件。

import {ModalComp} from '/your component location';

在视图中:

<mymodal></mymodal>

您必须保存对开放式模式的引用 window 以便稍后关闭它,例如:

public mr: NgbModalRef;
...
public openModal(content: string) {
    this.mr = this.modalService.open(content);
}
...
public closeModal() {
   this.mr.close();
}