4

I will close a bootstrap modal in my component via save when input is ok. I'm using angular v2.4.1 and ng-bootstrap 1.0.0-alpha.19

I've tried this solution but I get following error:

No provider for ViewContainerRef

I tried to add ViewContainerRef to my module.ts but I get following error:

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

Here the code in my component:

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

I also tried to use NgbModalRef like

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

But this is not working at all. I not even getting an error message.

2
  • did you add NgbModule in your ngModule? Commented Feb 6, 2017 at 13:37
  • yeah I add "NgbModule.forRoot()" in imports of @NgModule() Commented Feb 6, 2017 at 13:47

2 Answers 2

11

You have to save the reference to the openend modal window to close it later like:

public mr: NgbModalRef; ... public openModal(content: string) { this.mr = this.modalService.open(content); } ... public closeModal() { this.mr.close(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

thats it! Thank you soooooo much :3
This still works lovely. Don't forget to import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
-3

Using bootstrap modal Popup in angular 2 :

In Html : Just copy and paste bootstrap modal popup code.

In Ts File :

import {Component} from 'angular2/core'; @Component({ selector: 'mymodal', templateUrl: 'html page url' }) export class ModalComp { } 

Then you can use this component in any html view by importing in TS file.

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

In View :

<mymodal></mymodal> 

2 Comments

I have no problems with the template or opening the modal
The question states angular 2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.