components icon indicating copy to clipboard operation
components copied to clipboard

Dialog demo - error No component factory found for

Open hafquangbaox opened this issue 10 years ago • 46 comments

I use https://github.com/angular/material2/blob/master/src/demo-app/dialog/ for my project(create by angular-cli) and get this error. error_handler.js:45 EXCEPTION: No component factory found for JazzDialogErrorHandler.handleError @ error_handler.js:45next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:207Zone.runGuarded @ zone.js:113NgZoneImpl.runInnerGuarded @ ng_zone_impl.js:72NgZone.runGuarded @ ng_zone.js:236outsideHandler @ dom_events.js:26ZoneDelegate.invokeTask @ zone.js:236Zone.runTask @ zone.js:136ZoneTask.invoke @ zone.js:304 error_handler.js:50 ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:50next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:207Zone.runGuarded @ zone.js:113NgZoneImpl.runInnerGuarded @ ng_zone_impl.js:72NgZone.runGuarded @ ng_zone.js:236outsideHandler @ dom_events.js:26ZoneDelegate.invokeTask @ zone.js:236Zone.runTask @ zone.js:136ZoneTask.invoke @ zone.js:304 error_handler.js:51 Error: No component factory found for JazzDialog at NoComponentFactoryError.Error (native) at NoComponentFactoryError.BaseError [as constructor] (http://localhost:4200/main.bundle.js:7143:34) at new NoComponentFactoryError (http://localhost:4200/main.bundle.js:20178:16) at _NullComponentFactoryResolver.resolveComponentFactory (http://localhost:4200/main.bundle.js:20187:15) at NgModuleInjector.CodegenComponentFactoryResolver.resolveComponentFactory (http://localhost:4200/main.bundle.js:20212:35) at PortalHostDirective.attachComponentPortal (http://localhost:4200/main.bundle.js:33774:63) at MdDialogContainer.attachComponentPortal (http://localhost:4200/main.bundle.js:49832:45) at MdDialog._attachDialogContent (http://localhost:4200/main.bundle.js:70085:42) at MdDialog.open (http://localhost:4200/main.bundle.js:70042:21) at HomeComponent.open (http://localhost:4200/main.bundle.js:77695:38)ErrorHandler.handleError @ error_handler.js:51next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:207Zone.runGuarded @ zone.js:113NgZoneImpl.runInnerGuarded @ ng_zone_impl.js:72NgZone.runGuarded @ ng_zone.js:236outsideHandler @ dom_events.js:26ZoneDelegate.invokeTask @ zone.js:236Zone.runTask @ zone.js:136ZoneTask.invoke @ zone.js:304 zone.js:140 Uncaught Error: No component factory found for JazzDialogZone.runTask @ zone.js:140ZoneTask.invoke @ zone.js:304

Who can help me fix it, please

hafquangbaox avatar Oct 13 '16 04:10 hafquangbaox

Share some code, make also sure that the component you're trying to open is defined as entry in your NgModule's entryComponents: []

msegers avatar Oct 13 '16 07:10 msegers

Correct. What @msegers said. Placing components into the entryComponents portion of the NgModule declaration will allow Angular to compile those components into component factories and therefore allow the component resolver to add them to the internal map used for component resolution.

emilio-martinez avatar Oct 13 '16 08:10 emilio-martinez

Same issue here. The code I am trying is essentially the same demo "JazzDialog" at material2/src/demo-app/dialog/ on GitHub.

dfarago avatar Oct 13 '16 15:10 dfarago

We are on RC5. Will this only work with RC-final?

dfarago avatar Oct 13 '16 16:10 dfarago

I did it. Edit my app.module.ts like https://github.com/angular/material2/blob/master/src/demo-app/demo-app-module.ts

entryComponents: [ JazzDialog, ]

and in my component need show dialog, i edit it like https://github.com/angular/material2/tree/master/src/demo-app/dialog.

hafquangbaox avatar Oct 13 '16 16:10 hafquangbaox

That seemed to work! Thanks!

dfarago avatar Oct 13 '16 17:10 dfarago

OK, close it

hafquangbaox avatar Oct 13 '16 17:10 hafquangbaox

FYI, it has to be the main module, at least for me, it wouldn't work in a sub-module.

PostImpatica avatar Dec 22 '16 15:12 PostImpatica

@lozthiensu This issue came up again... I worked before without doing this and now with new angular updates this broke again or is this supposed to be intended use.

xtianus79 avatar Jan 05 '17 19:01 xtianus79

I also had issues with the error "No component factory". I tried declaring the component in both the same module as the parent, as well as the AppModule. Same error in both cases.

The demo on material.angular.io declares the dialog component in the same file. Maybe it's an issue for angular if you declare the component in a separate file, as you would do with any other component.

mark-langer avatar Jan 15 '17 08:01 mark-langer

@lozthiensu you should reopen this issue because there is not clear documentation on this.

xtianus79 avatar Jan 17 '17 15:01 xtianus79

Also having this issue. Not sure how I'm supposed to get this to work in a lazy-loaded module. If i put the component in the lazy-loaded modules entryComponents array i still get:

No component factory found for xxxComponent. Did you add it to @NgModule.entryComponents

chrissena avatar Jan 19 '17 10:01 chrissena

angular-cli: 1.0.0-beta.26 Angular 2.4.5 Confirmed, you cannot create a Dialog Component in a lazy-loaded module (Router loadChildren string value).

Is this an Angular project issue, or should this material2 issue be re-opened?

arlowhite avatar Jan 30 '17 21:01 arlowhite

SOLVED with lazy loaded module:

i have done this, i have added GenerateApiKeyComponent (dialog component) to delcarations array and in entryComponent array and its working fine. its opening dialog.

 declarations: [
    AdminComponent,
    ProjectsComponent,
    ProjectComponent,
    ProjectDetailsComponent,
    DashboardComponent,
    GenerateApiKeyComponent
  ],
  entryComponents: [
    GenerateApiKeyComponent
  ],
  providers: [ProjectService]
})

export class AdminModule {

}

imVinayPandya avatar Jan 31 '17 08:01 imVinayPandya

Doing what @imVinayPandya did did not work for me. I'm still getting the No component factory found for <component>. error. Putting declaration and entryComponent definitions into the base AppModule lets it work. So it does seem to be an issue with this project and lazy loaded modules. Declaring components in lazy loaded modules works in https://github.com/ng-bootstrap/ng-bootstrap

adamk33n3r avatar Feb 07 '17 02:02 adamk33n3r

Looks like it will be fixed in next release #2556

adamk33n3r avatar Feb 07 '17 03:02 adamk33n3r

In case @lozthiensu instructions aren't 100% for people new to ng2+ and Material 2....

Keep in mind, if you are following the material demo code (as is @lozthiensu ) then your dialogs (JazzDialog, etc.) are individual child components, which means that you'll need to also add them to your @ngModule.declarations along with the parent component that your child components are residing in. After that, you'll simply need to add your dialog child component(s) (i.e. JazzDialog) to @ngModule.entryComponents.

If you aren't familiar with entryComponents and how they differ from regular components I highly recommend spending 2 minutes reading this => https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-entry-component-defined

Props to @lozthiensu for sharing his repo and the @angular team for having legit examples in their repo and thorough docs online. I was struggling with this one for a minute.

offroadkev avatar Apr 24 '17 06:04 offroadkev

In my case, I was not only missing

entryComponents: [
    MyDialogComponent,
  ],

(see @msegers answer)

but also

import { MaterialModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
...
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MaterialModule,
   ...
  ],

inside app.module.ts

zurfyx avatar Jun 01 '17 19:06 zurfyx

Thank you @zurfyx, I had the same problem and I solved it adding the entryComponents tag in the main module.

jhoncamargo avatar Jun 05 '17 03:06 jhoncamargo

Hi, @zurfyx , i'm using the "@angular/material": "^2.0.0-beta.12" and i'm facing the same issue, however MaterialModule is nolonger exported by @angular/material, How can i use your fix for the current @angular/material

wizlif avatar Dec 01 '17 11:12 wizlif