shlomiassaf / ngx-modialog

Modal / Dialog for Angular
http://shlomiassaf.github.io/ngx-modialog
MIT License
686 stars 242 forks source link

Use angular2-modal on a non-top level component #298

Open dolanmiu opened 7 years ago

dolanmiu commented 7 years ago

All examples I've seen is using this library on a top level component (for example app-component). But I want to use it in a nested component. For example, I want to open a login modal inside the navbar component.

How would I do this? It gives an error every time I try to do it in a nested component:

EXCEPTION: Uncaught (in promise): Error: Default view container not set. Add the "defaultOverlayTarget" directive to the application root component template (e.g: <span defaultOverlayTarget></span>. You can also set it manually using the "Overlay" service "defaultViewContainer" property.
gedarufi commented 7 years ago

Just add this to your html on the component container

<div class="container-fluid" **defaultOverlayTarget**></div>

ladmini commented 7 years ago

@gedarufi There will be some grammatical errors with your instruction. Any updates?

bcharfi commented 7 years ago

In your root template add this: <span defaultOverlayTarget></span> Set defaultViewContainer property is on the modal.overlay object in the root component constructor:

constructor(vcRef: ViewContainerRef, public modal: Modal) {
    modal.overlay.defaultViewContainer = vcRef;
}