Angular2 ng-bootstrap模块在组件中关闭

tro*_*lol 4 modal-dialog ng-bootstrap angular

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

我试过这个解决方案,但是我得到了以下错误:

没有ViewContainerRef的提供者

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

类型'typeof ViewContainerRef'不能分配给'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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还试图用NgbModalRef

  constructor(public modalRef: NgbModalRef) {}

 public closeModal() {
    this.modalRef.close();
    this.modalRef.dismiss();
}
Run Code Online (Sandbox Code Playgroud)

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

小智 10

你必须保存对openend模态窗口的引用,以便稍后关闭它:

public mr: NgbModalRef;
...
public openModal(content: string) {
    this.mr = this.modalService.open(content);
}
...
public closeModal() {
   this.mr.close();
}
Run Code Online (Sandbox Code Playgroud)