2

I try to use NgbModalRef to close a Modal (That open with NgbModal) and in some components I see the Error:

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(MyModule)[NgbModalRef -> NgbModalRef -> NgbModalRef -> NgbModalRef -> NgbModalRef]: NullInjectorError: No provider for NgbModalRef!


Note1:I have imported and Injected NgbModalRef and NgbModal in my component and tried using NgbModule in my appModule and it didnt Work! Note2:I saw somewhere that people say to write NgbModalRef in appModule provider,and i dont think its the right way because its not a Service!! Can anyone please help..

arunes
  • 3,464
  • 2
  • 21
  • 31
  • any other module you are using other than app.module ? – Developer May 11 '21 at 16:19
  • You would get more help if you add your code for us to be able to reproduce the problem. Wild guess: NbDialogModule it's not included in the module of the component you are using the NgbModalRef as @GaurangDhorda said. – arunes May 11 '21 at 16:23
  • actually I use like 10 or more modules and appModule is my main Module,and I got this Error in some of them not All (codes to open and close modals are same in all )! @GaurangDhorda – mohammadreza May 11 '21 at 16:47
  • import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; ... constructor( private modalService: NgbModal, public activeModal: NgbActiveModal) const modalRef = this.modalService.open(StudentComponent as Component); dismiss() { this.activeModal.dismiss('cancel'); } @arunes – mohammadreza May 11 '21 at 16:48
  • That's okay.. but have you used model component in other module to ? @mohammadreza – Developer May 11 '21 at 16:51
  • yes @GaurangDhorda – mohammadreza May 11 '21 at 16:55
  • Then you need to imoort NgbModule inside that module too! @mohammadreza – Developer May 11 '21 at 16:57
  • i just did what you said and it didnt work @GaurangDhorda – mohammadreza May 11 '21 at 17:27

1 Answers1

0

i solved this problem using this reference.

https://github.com/ng-bootstrap/ng-bootstrap/issues/1832#issuecomment-331798666

from private modalReference: NgbModalRef;

to private modalReference: any;