6

Here is a blank angular 7 project in stackblitz, in Angular 6 I used constructor(private bsModalRef: BsModalRef) so I can pass values to my child popup component.

But when I update to angular 7, it said Module not found: Error: Can't resolve 'ngx-bootstrap/modal/bs-modal-ref.service'.

In stackblitz, it asked me to install ngx-bootstrap but I already installed.

Any idea?

3
  • Have you imported the package module? Commented Nov 9, 2018 at 17:23
  • Yes. import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';. Commented Nov 9, 2018 at 17:29
  • 1
    You're injecting BsModalRef while you should be injecting BsModalService. Check my answer for more details. Commented Nov 9, 2018 at 17:49

3 Answers 3

12

First thing is you need to change your import in app.component.ts from

import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

to

import { BsModalRef } from 'ngx-bootstrap';

then you will have to provide providers in app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { ModalModule } from 'ngx-bootstrap/modal';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';

@NgModule({
  imports:      [ BrowserModule, FormsModule,ModalModule.forRoot(),
  BsDropdownModule.forRoot() ],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ],
  providers: [BsModalRef]
})
export class AppModule { }

working STACKBLITZ

Sign up to request clarification or add additional context in comments.

2 Comments

It works but why the doc said from 'ngx-bootstrap/modal/bs-modal-ref.service'; and no probiders.
When ever you're using a service you need to provide them in provider otherwise angular won't be able to understand that it's a service
1

Please

import {BsModalRef} from 'ngx-bootstrap/modal/bs-modal-ref.service'

and inject the BsModalService as a dependency.

Comments

0

According to the docs here, you'll need to inject the BsModalService as a dependency. But you're injecting BsModalRef.

Here's the correct syntax:

import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
...
modalRef: BsModalRef;
constructor(private modalService: BsModalService) {}

openModal(template: TemplateRef<any>) {
  this.modalRef = this.modalService.show(template);
}

Here's a Working StackBlitz Example for your ref.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.