ngx-bootstrap: fix(positioning): angular 2.2 modal.show is throwing issue
Angular v2.2 broke our get root view component ref hack temporary workaround:
- Add
ComponentsHelpertoAppModule(root module)
import {ComponentsHelper} from 'ng2-bootstrap/ng2-bootstrap'
// ...
providers: [{provide: ComponentsHelper, useClass: ComponentsHelper}],
- Set root view component ref explicitly in
AppComponent(root component)
public constructor(componentsHelper:ComponentsHelper, vcr:ViewContainerRef) {
componentsHelper.setRootViewContainerRef(vcr);
}
I am facing an issue with my ng2-bootstrap modals. For some reason, I am not able to show them.
Here is my HTML :
<button (click)="testModal.show()">Show this modal</button>
<div bsModal
#testModal="bs-modal"
class="modal fade"
tabindex="-1"
role="dialog"
aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-body">
TEST
</div>
</div>
</div>
</div>
Here is my NgModule:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MODAL_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';
@NgModule({
declarations: [ MODAL_DIRECTIVES],
exports: [ MODAL_DIRECTIVES],
imports: [ BrowserModule,
FormsModule,
HttpModule ],
})
export class SharedModule {}
I see this error in the log console : Error: Uncaught (in promise): Token must be defined! and the modal does not show up
I am running Angular 2 RC5. Is there something I am missing here?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 8
- Comments: 65 (31 by maintainers)
Commits related to this issue
- chore(demo): fix for ng2 v2.2 #986 — committed to valor-software/ngx-bootstrap by valorkin 8 years ago
- DE2188: Hack for ng2-bootstrap bug https://github.com/valor-software/ng2-bootstrap/issues/986#issuecomment-262293199 — committed to crdschurch/crds-signin-checkin by IngageKrizJ 8 years ago
- fix for https://github.com/valor-software/ng2-bootstrap/issues/986 — committed to fragaria/ng2-f-template-spa by tsupka 8 years ago
Thanks @kraikill
For me only your solution works. Its also the easiest patch for now.
Just to clarify for others, copy/paste the following in your main component (appComponent)
it should fix the issue for NG2 v2.2.1 until an official patch is released by NG2-bootstrap team š
ng2 v2.2 added a breaking change to internals working on it
So after talking to @valorkin he let me know that the answer (for now) is at the very top of this thread. I just failed at reading the entire thing lol. For anyone not wanting to read through the whole post you need to do two things.
Add the following to your constructor.
And add the following to your imports
https://github.com/valor-software/ng2-bootstrap/issues/986#issue-177218652
@valorkin is there any reason not to make a
<modal-outlet></modal-outlet>like the angular router does? This will be really clear to everybody and you would stop calling it the hack because it is just how angular does it.Then it is a component where all the modals (or at least the backdrop) can be rendered in isolated environment. Everybody is happy and it wonāt ever be a problem again.
e.g. app component template would be similar to
Maybe the same could be applied for the tooltips
Hi,
I am also seeing the same issue. It seems to be a timing issue. When I do the following:
Then I get the below error:
It seems that there are no components registered in
this.applicationRef.componentsat this stage, which is why the above error occurs⦠Moving thethis.loginModal.modal.show();into a click directive, then it works fineā¦@valorkin @conor-mac-aoidh usually +1 is not the best thing to do but I cannot avoid it. Thank you for your contribution to the development of angular2 and its community š ! I believe this project helps lots of people and makes angular2 closer to the not experienced users. Donāt listen to the ugly people, listen to the good ones š
Just wanted to verify https://github.com/escarabin. Facing a similar error message with ng2-bootstrap version 1.1.12 the moment I try to invoke:
someModal.show();Works fine when loaded in the browser, but fails when executed in a test run by karma. The interesting thing is: Running the following while testing does not lead to any errors:
someModal.hide();Iām sure I miss something for karma.conf.js or karma.shim.js but I do not have any idea what it could be.I updated to v1.1.6 tough. The error has now changed to āapplicationRef instance not foundā when I try to open a modal.
@valorkin Thanks for your time on this project it is very helpful to have this module! Keep up the good work!
I rewrote component injector, testing⦠will be able to publish late today or early tomorrow
Simple workaround: override - ComponentsHelper.getRootViewContainerRef
The problem of access to the to undefined _hostElement and we need to get the correct ViewContainerRef. Replace:
on (if viewContainerRef exists in app component)
This solution works fine for me
same here, amazing job and THANKS for the effort, sure is appreciated!! Sean
While the given workaround did not fix the issue, here is the version that works for me.
ok, guys new injection service is working š so I will be updating components to use new service most probably I will do release with
nexttag, on Monday when QA will approve this version it will marked as latestI agree wtih @smnbbrv. The outlet approach seems much more robust. Having core functionality depend on brittle hacks really shakes my confidence in the whole framework. The workaround suggested is also not working for me, as I am using system.js, and migrating to webpack is not an option for me.
Really sorry about that. I just updated to v1.1.5 and the error has changed to āCannot read property āinstanceā of undefinedā
Here is a full screenshot of the log:
I hope it is not too late to request some help on thisā¦
Forget about directives, use exported modules