Ngx-bootstrap: How to get access to `BsModalRef` in opened modal component?

Created on 3 Aug 2017  路  5Comments  路  Source: valor-software/ngx-bootstrap

I really appreciate ngx-bootstrap modal work before going to post my question.
I found one problem in following guide ngx-bootstrap-modal with service - http://valor-software.com/ngx-bootstrap/#/modals#service-nested

Having problem with this statement If you passed a component to .show() you can get access to opened modal by injecting BsModalRef.

export class ModalContentComponent {
public title: string;
public list: any[] = [];
constructor(public bsModalRef: BsModalRef) {} // How do you get access to bsModalRef here
}

I try running this example, but I never get bsModalRef in opened modal.

I also try passing bsModalRef through content, but it work with one modal and failed to work with nested modal.

Is there any other way to pass bsModalRef in ModalContentComponent ?

comp(modal)

Most helpful comment

Gotcha.

I was using import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class'; as per http://valor-software.com/ngx-bootstrap/#/modals#service-nested

But I found import { BsModalRef } from 'ngx-bootstrap'; in your plunker.

Now, I can access to bsModalRef anywhere regardless of my ModalContentComponent location.

Thanks a lot.

All 5 comments

Hi! Didn't exactly get what's the issue here. There's no need to pass bsModalRef to ModalContentComponent, you only need to inject it in its constructor.
Check this out - https://plnkr.co/edit/tyDezs2LswVHyU2l9fwp?p=preview

@IlyaSurmay Thanks for reply.
In my case I'm writing ModalContentComponent in anther file. I can get bsModalRef when I use both of component in same file but failed when using different files for AppComponent & ModalContentComponent .

So basically, I need bsModalRef in ModalContentComponent which is in another file.

Gotcha.

I was using import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class'; as per http://valor-software.com/ngx-bootstrap/#/modals#service-nested

But I found import { BsModalRef } from 'ngx-bootstrap'; in your plunker.

Now, I can access to bsModalRef anywhere regardless of my ModalContentComponent location.

Thanks a lot.

instead of

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

you can write directly
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';

the thing to solve this you need to do is in your app.module.ts

import { ModalModule, BsModalRef } from 'ngx-bootstrap/modal';

     imports: [
        ModalModule.forRoot()
   ],
     providers: [
        BsModalRef
   ],

I hope this will Help You.

Because it worked for me.

thanks a lot!
i forgot BsModalRef in providers

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pgeyman picture pgeyman  路  3Comments

RolfVeinoeSorensen picture RolfVeinoeSorensen  路  3Comments

haisaco picture haisaco  路  3Comments

Scooviese picture Scooviese  路  3Comments

ctrl-brk picture ctrl-brk  路  3Comments