Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RC5 Modal not working in multiple components #854

Closed
hassanasad opened this issue Aug 18, 2016 · 51 comments
Closed

RC5 Modal not working in multiple components #854

hassanasad opened this issue Aug 18, 2016 · 51 comments

Comments

@hassanasad
Copy link

Hi,

I am using Angular RC5 and am able to open modal box in the root component (app-component) using viewProviders: [BS_VIEW_PROVIDERS] and keeping the modal box and button in the component template.

However when i do the same for other components, it triggers an error:

Error: Uncaught (in promise): No provider for AppComponent!

Any ideas how to fix this please?

@jimmythesaint82
Copy link

I'm also getting this error

@rspenc29
Copy link

Adding BS_VIEW_PROVIDERS to providers instead of viewProviders seems to fix this but I haven't really dug into the code to see why.

@hassanasad
Copy link
Author

@rspenc29 wow, let me try it with your technique :)

@hassanasad
Copy link
Author

@rspenc29 I tried, its not working for me. Everything works fine if the modal box is in the root component (appComponent). I get error when i put it in some other component.

This is the error that i get:

EXCEPTION: Error: Uncaught (in promise): No provider for AppComponent!

P.S: Are you using RC5 and webpack?

@rspenc29
Copy link

This works for me. I'm also using the feat-rc5 branch

@NgModule({
    imports: [
        // ...
        ModalModule,
    ],
    declarations: [
        // ...
    ],
    providers: [ BS_VIEW_PROVIDERS ],
})

export class AppModule {}

I did however have an issue with the backdrop and for now i'm using [config]="{backdrop: false}" until i have time to troubleshoot it or until an update is released.

@hassanasad
Copy link
Author

Thanks @rspenc29 - I will give it a shot with feat-rc5 branch in a little while.

@jimmythesaint82
Copy link

Yeah that fix doesn't work for me still get
EXCEPTION: Error: Uncaught (in promise): No provider for App

on feat-rc5

@noxborough
Copy link

noxborough commented Aug 19, 2016

I'm experiencing the same issue with RC5. Only i'm trying to access Modals from a lazy loaded component inside of an NgModule. The offending code in ng2-bootstrap is inside the components-helper.service.js (see below)

ComponentsHelper.prototype.getRootViewContainerRef = function () {
    // The only way for now (by @mhevery)
    // https://github.com/angular/angular/issues/6446#issuecomment-173459525
    // this is a class of application bootstrap component (like my-app)
    var classOfRootComponent = this.applicationRef.componentTypes[0];
    // this is an instance of application bootstrap component
    var appInstance = this.injector.get(classOfRootComponent);
    return appInstance.viewContainerRef;
};

The classOfRootComponent has a reference to my root component type (for most people this is AppComponent). The injector is not the root injector, but rather the injector associated with my lazy loaded module. So I'm guessing it does not have access to the root component instance of AppComponent. And so it bombs out.

I have not been able to figure out a work around.

@a5hik
Copy link

a5hik commented Aug 23, 2016

same issue here.

@a5hik
Copy link

a5hik commented Aug 23, 2016

Is it working with feat-rc5? how do you got that via npm install

@kamikaze
Copy link

+1

@noxborough
Copy link

noxborough commented Aug 23, 2016

I was able to get it to work using the feat-rc5 branch after some refactoring to import the ng2-bootstrap ModalModule. But it only worked after i set this.myModal.config.backdrop = false (instead of "static").

Which seems to be the same solution that @rspenc29 found. I think if you use the "static" value, it tries to find the root component in the injector and can't resolve it.

@a5hik - i don't think you can install it via npm. i just downloaded it, and extracted it into my project until they do a full RC5 compatible release.

@rspenc29
Copy link

To use feat-rc5, just do the following...

cd node_modules
git clone https://github.com/valor-software/ng2-bootstrap.git
cd ng2-bootstrap
git checkout feat-rc5
npm install

@a5hik
Copy link

a5hik commented Aug 23, 2016

Hi now I'm able to use feat-rc5, I'm importing the ng2-bootstrap module like below.

import {DatepickerModule} from 'ng2-bootstrap/components/datepicker/datepicker.module'; import {DropdownModule} from 'ng2-bootstrap/components/dropdown/dropdown.module'; import {ModalModule} from 'ng2-bootstrap/components/modal/modal.module';

However I get the same error when I try to use the modal ..in my lazy loaded module.

I do specify the providers: [BS_VIEW_PROVIDERS] in my module and removed the 'viewProviders` from the component code as suggested.

But I still face the same error.

No provider for AppComponent!

I just wonder what I'm missing here, or how it works for others.

@rspenc29
Copy link

The error is caused by the backdrop. Just disable it until they fix it.

@a5hik
Copy link

a5hik commented Aug 23, 2016

I started working after I change `[config]="{backdrop: 'static'}" to [config]="{backdrop: false}"

@a5hik
Copy link

a5hik commented Aug 23, 2016

thanks @rspenc29 @noxborough

@a5hik
Copy link

a5hik commented Aug 23, 2016

I face other issue, that If I run in --prod mode.. I get the below errors..

`Can't bind to 'activeDate' since it isn't a known property of 'datepicker-inner'.

  1. If 'datepicker-inner' is an Angular component and it has 'activeDate' input, then verify that it is part of this module.
  2. If 'datepicker-inner' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
    ("
    <datepicker-inner [ERROR ->][activeDate]="activeDate"
    (update)="onUpdate($event)"
    [da"): a@1:22
    Can't bind to 'datepickerMode' since it isn't a known property of 'datepicker-inner'.
  3. If 'datepicker-inner' is an Angular component and it has 'datepickerMode' input, then verify that it is part of this module.
  4. If 'datepicker-inner' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
    ("r [activeDate]="activeDate"
    (update)="onUpdate($event)"
    [ERROR ->][datepickerMode]="datepickerMode"
    [initDate]="initDate"
    ["): a@3:22
    Can't bind to 'initDate' since it isn't a known property of 'datepicker-inner'.
  5. If 'datepicker-inner' is an Angular component and it has 'initDate' input, then verify that it is part of this module.
  6. If 'datepicker-inner' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
    ("e)="onUpdate($event)"
    [datepickerMode]="datepickerMode"
    [ERROR ->][initDate]="initDate"
    [minDate]="minDate"
    [maxDate]="maxD"): a@4:22
    Can't bind to 'minDate' since it isn't a known property of 'datepicker-inner'.`

@batchu
Copy link

batchu commented Aug 23, 2016

@a5hik See here angular/angular#10618

Also, can you post a quick guide on how to use feat-rc5? I updated my package.json to
"ng2-bootstrap": "https://github.com/valor-software/ng2-bootstrap.git#feat-rc5",

But now I only see .md files downloaded. I do not see any .ts files.
So I checked out the project in git as rspenc29 suggested. my npm build fails. How do I get this to work?

@jimmythesaint82
Copy link

You need to pull from git, detailed above from rob.
On Tue, 23 Aug 2016 at 22:10, Prashanth Batchu notifications@github.com
wrote:

@a5hik https://github.com/a5hik See here

Also, can you post a quick guide on how to use feat-rc5? I updated my
package.json to
"ng2-bootstrap": "
https://github.com/valor-software/ng2-bootstrap.git#feat-rc5",

But now I only see .md files downloaded. I do not see any .ts files.
So I checked out the project in git as rspenc29 suggested. my npm build
fails. How do I get this to work?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#854 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACUqVK65Hy8SCLJnNT9Srak1xnP9D16aks5qi1PBgaJpZM4JnKtZ
.

@a5hik
Copy link

a5hik commented Aug 24, 2016

I tried with the webpack upgrade to get the prod version working..But ended up in having the below issue. I used the angular cli from the master repo.

angular/angular-cli#1636

@valorkin
Copy link
Member

valorkin commented Sep 2, 2016

v 1.1.0 - rc.5
v 1.1.1 - rc.6
should work fine now, please confirm

@hassanasad
Copy link
Author

I am using v1.1.1 with RC6 and i am still getting the same error:
"No provider for AppComponent!"

Perhaps i am missing anything?

I do have to hack in the main AppComponent:

import { ViewContainerRef } from '@angular/core';
...
constructor(
        private renderer: Renderer, 
        public viewContainerRef:ViewContainerRef,
        ) {
        this.viewContainerRef = viewContainerRef;
    }

@valorkin
Copy link
Member

valorkin commented Sep 2, 2016

ng2-bootstrap doesn't use or requires any AppComponent
Please do full text search in your app

@valorkin valorkin closed this as completed Sep 2, 2016
@hassanasad
Copy link
Author

doing this in for the modal fixes the issue [config]="{backdrop: false}" - Perhaps its looking for a backdrop in the main AppComponent?

@valorkin valorkin reopened this Sep 2, 2016
@hassanasad
Copy link
Author

AppComponent is my main component (which has the hack as mentioned in the comment above)

@dhardtke
Copy link

dhardtke commented Sep 7, 2016

I don't have a BS_VIEW_PROVIDERS constant with the latest version, so I guess it is exclusive to the rc5 branch.

@hassanasad
Copy link
Author

Yeah its not available in the latest version. For now [config]="{backdrop: false}" is the only way i found to get it working.

@amirjelo
Copy link

amirjelo commented Sep 9, 2016

@hassanasad your solution just worked for the first time that modal opened , next time you open the modal , this error cames up : Uncaught TypeError: Cannot set property stack of [object Object] which has only a getter

@noxborough
Copy link

noxborough commented Sep 13, 2016

@valorkin
I tested with RC6 and ng2-bootstrap v1.1.2.

I can confirm that config.backdrop = "static works fine with eagerly loaded components.

But if the component that creates the modal dialog is part of a module that is lazy loaded (and it sets config.backdrop = "static"), than I still get the same error: "No provider for AppComponent!"

As i stated in an earlier post, the error manifests itself in components-helper.service.js when trying to get the root app component from the injector.

ComponentsHelper.prototype.getRootViewContainerRef = function () {
    // The only way for now (by @mhevery)
    // https://github.com/angular/angular/issues/6446#issuecomment-173459525
    // this is a class of application bootstrap component (like my-app)
    var classOfRootComponent = this.applicationRef.componentTypes[0];
    // this is an instance of application bootstrap component
    var appInstance = this.injector.get(classOfRootComponent);
    return appInstance.viewContainerRef;
};

Tahler added a commit to Tahler/nu-code-web that referenced this issue Sep 13, 2016
Subscribed to this thread to follow the issue: valor-software/ngx-bootstrap#854
Tahler added a commit to Tahler/nu-code-web that referenced this issue Sep 13, 2016
Subscribed to this thread to follow the issue: valor-software/ngx-bootstrap#854
@valorkin
Copy link
Member

OK, I see where is issue came from I will test fix on @flibbertigibbet repo, if work I will publish fix today

@valorkin
Copy link
Member

Guys need a sample repo with issue to reproduce, thanks in advance. Seems I have found a solution, but need to fix it first

@valorkin
Copy link
Member

Test it *

@noxborough
Copy link

@valorkin
I tested with RC7 and ng2-bootstrap v1.1.3

Now when i use a modal dialog inside of a component that is lazy loaded (and i have config.backdrop = "static"). It gets caught in an infinite loop.

In the code below, appInstance is getting set to false because it is not found in the injector. And the injector's parentInjector is undefined.

ComponentsHelper.prototype.getRootViewContainerRef = function (_injector) {
    // The only way for now (by @mhevery)
    // https://github.com/angular/angular/issues/6446#issuecomment-173459525
    // this is a class of application bootstrap component (like my-app)
    var classOfRootComponent = this.applicationRef.componentTypes[0];
    // this is an instance of application bootstrap component
    var appInstance;
    var injector = _injector;
    while (!appInstance) {
        appInstance = injector.get(classOfRootComponent, false);
        if (!appInstance && injector.parentInjector) {
            injector = injector.parentInjector;
        }
    }
    return appInstance.viewContainerRef;
};

@valorkin
Copy link
Member

I really need a code sample, where it fails

On Thu, Sep 15, 2016, 01:12 noxborough notifications@github.com wrote:

@valorkin https://github.com/valorkin
I tested with RC7 and ng2-bootstrap v1.1.3

Now when i use a modal dialog inside of a component that is lazy loaded
(and i have config.backdrop = "static"). It gets caught in an infinite
loop
.

In the code below, appInstance is getting set to false because it is
not found in the injector. And the injector's parentInjector is
undefined.

ComponentsHelper.prototype.getRootViewContainerRef = function (_injector) {
// The only way for now (by @mhevery)
// angular/angular#6446 (comment)

// this is a class of application bootstrap component (like my-app)
var classOfRootComponent = this.applicationRef.

componentTypes[0];
// this is an instance of application bootstrap component
var appInstance;
var injector = _injector;
while (!appInstance) {
appInstance = injector.get(classOfRootComponent, false);
if (!appInstance && injector.parentInjector) {
injector = injector.parentInjector;
}
}
return appInstance.viewContainerRef;
};


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#854 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABDk4_s4CrR5geCNcmOoiRbQh68Br8Ynks5qqHFAgaJpZM4JnKtZ
.

@valorkin valorkin reopened this Sep 15, 2016
@valorkin valorkin mentioned this issue Sep 15, 2016
@noxborough
Copy link

haven't ever put a plunker together. working on putting one together that demonstrates the issue

@noxborough
Copy link

noxborough commented Sep 15, 2016

@valorkin
Ok here goes. This plunker should demonstrate the issue. As soon as you click on the "My Modal" button, it should get caught in the infinite loop.

https://plnkr.co/edit/PLF3dN?p=info

However, if you go into my-modal.component.ts and switch from
this.myModal.config.backdrop = "static";
to
this.myModal.config.backdrop = false;
it should work.

@tungphuong
Copy link

Confirmed, It worked well. Thanks

@valorkin
Copy link
Member

@noxborough thank you so much! working on fix

@valorkin
Copy link
Member

@noxborough your plunker was so helpful !
publishing fix in a minutes

@valorkin
Copy link
Member

guys, 1.1.5 is published please confirm that fix is working properly
thanks in advance
PS: don't forget to add viewContainerRef to main app component
http://valor-software.com/ng2-bootstrap/#/modals

@hassanasad
Copy link
Author

hassanasad commented Sep 16, 2016

WOW -- Thanks @valorkin after much effort from you and enough complaining from us, its finally working :)

@noxborough
Copy link

@valorkin
no problem

I edited the plunker to use 1.1.5, and it didn't work. But only because i forgot to add the viewContainerRef hack in. All is well now. thanks for fixing.

@valorkin
Copy link
Member

:happy:

On Fri, Sep 16, 2016, 17:56 noxborough notifications@github.com wrote:

@valorkin https://github.com/valorkin
no problem

I edited the plunker to use 1.1.5, and it didn't work. But only because i
forgot to add the viewContainerRef hack in. All is well now. thanks for
fixing.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#854 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABDk44tSYEh2A91r2LHkQpTA7flInt22ks5qqq4igaJpZM4JnKtZ
.

@tungphuong
Copy link

Updated to 1.1.5, it worked well, dont need to set this.myModal.config.backdrop = false;
Thanks @valorkin

jtomaszewski pushed a commit to jtomaszewski/ng2-bootstrap that referenced this issue Oct 22, 2016
jtomaszewski pushed a commit to jtomaszewski/ng2-bootstrap that referenced this issue Oct 22, 2016
Gxerxes pushed a commit to Gxerxes/ng2-bootstrap that referenced this issue Oct 28, 2016
Gxerxes pushed a commit to Gxerxes/ng2-bootstrap that referenced this issue Oct 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests