Angular2 material dialog has issues - Did you add it to @NgModule.entryComponents?

前端 未结 11 2126
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 06:37

I am trying to follow the docs on https://material.angular.io/components/component/dialog but I cannot understand why it has the below issue?

I added the below on my

相关标签:
11条回答
  • 2020-12-04 06:56

    You need to add dynamically created components to entryComponents inside your @NgModule

    @NgModule({
      declarations: [
        AppComponent,
        LoginComponent,
        DashboardComponent,
        HomeComponent,
        DialogResultExampleDialog        
      ],
      entryComponents: [DialogResultExampleDialog]
    

    Note: In some cases entryComponents under lazy loaded modules will not work, as a workaround put them in your app.module (root)

    0 讨论(0)
  • 2020-12-04 06:56

    You must add it to entryComponents, as specified in the docs.

    @NgModule({
      imports: [
        // ...
      ],
      entryComponents: [
        DialogInvokingComponent, 
        DialogResultExampleDialog
      ],
      declarations: [
        DialogInvokingComponent,   
        DialogResultExampleDialog
      ],
      // ...
    })
    

    Here is a full example for an app module file with a dialog defined as entryComponents.

    0 讨论(0)
  • 2020-12-04 06:58

    While integrating material dialog is possible, I found that the complexity for such a trivial feature is pretty high. The code gets more complex if you are trying to achieve a non-trivial features.

    For that reason, I ended up using PrimeNG Dialog, which I found pretty straightforward to use:

    m-dialog.component.html:

    <p-dialog header="Title">
      Content
    </p-dialog>
    

    m-dialog.component.ts:

    @Component({
      selector: 'm-dialog',
      templateUrl: 'm-dialog.component.html',
      styleUrls: ['./m-dialog.component.css']
    })
    export class MDialogComponent {
      // dialog logic here
    }
    

    m-dialog.module.ts:

    import { NgModule } from "@angular/core";
    import { CommonModule } from "@angular/common";
    import { DialogModule } from "primeng/primeng";
    import { FormsModule } from "@angular/forms";
    
    @NgModule({
      imports: [
        CommonModule,
        FormsModule,
        DialogModule
      ], 
      exports: [
        MDialogComponent,
      ], 
      declarations: [
        MDialogComponent
      ]
    })
    export class MDialogModule {}
    

    Simply add your dialog into your component's html:

    <m-dialog [isVisible]="true"> </m-dialog>
    

    PrimeNG PrimeFaces documentation is easy to follow and very precise.

    0 讨论(0)
  • 2020-12-04 07:01

    In my case, I added my component to declarations and entryComponents and got the same errors. I also needed to add MatDialogModule to imports.

    0 讨论(0)
  • 2020-12-04 07:01

    If someone needs to call Dialog from services here is how to solve the issue. I agree with some of above answer, my answer is for calling dialog in services if someone may face issues on.

    Create a service for example DialogService then move your dialog function inside the services and add your dialogservice in the component you call like below code:

     @Component({
      selector: "app-newsfeed",
      templateUrl: "./abc.component.html",
      styleUrls: ["./abc.component.css",],
      providers:[DialogService]
    })
    

    otherwise you get error

    0 讨论(0)
提交回复
热议问题