2

I'm using Material design components for my Angular application. For a few parts of my application, I'm opening a dialog/modal that covers the whole viewport and I want to nest components within that modal.

For example, here is the code to open a dialog/modal:

  openUserAccountModal(): void {
   const dialogRef = this.dialog.open(MyAccountModalComponent, {
    width: '100vw',
    height:  '100vh',
    maxWidth: '100vw',
    maxHeight: '100vh',
    hasBackdrop: false
  });

   dialogRef.afterClosed().subscribe(result => {
    console.log('The dialog was closed');
  });
}

When this dialog/modal opens, I have two main sections, a side navigational menu with different section names and the content I want to display for each section. I'm using [NgSwitch] to display the different sections. Here is a code snippet of what I have now:

 <div *ngSwitchCase="'My Profile'">
            <div class="my-profile">
                <h4 class="my-profile__section-title">My Profile</h4>
                <p class="my-profile__section-description">Add details about your education, industry 
                   of interest,
                    etc.
                </p>
                <mat-card class="my-profile__user-card u-no-shadow">
                    <div class="edit-btn__wrapper">
                        <button *ngIf="!editMode" mat-flat-button color="primary" class="edit-btn"
                            (click)="editUserData()">Edit</button>
                    </div>
                    <div *ngIf="editMode" class="edit-form">
                        <form action="" class="edit-form__image">
                            <mat-list class="edit-form__list">
                                <mat-list-item class="my-profile__user-list--item image-item">
                                    <img class="my-profile__user-list--image" matListAvatar 
                                 [src]="userData.image"
                                        alt="...">
                                </mat-list-item>
                            </mat-list>
                        </form>

Now, onto the question: I want to organize each section into its own component, however, the component won't display when I do so. This is a code snippet of what I want:

<div *ngSwitchCase="'My Profile'">
   <app-my-profile></app-my-profile>

Yet, this code won't render the component. My assumption is that is has to do with it being rendered within the dialog/modal. Any idea of how I can achieve this goal?

Dave Guerrero
  • 93
  • 1
  • 10
  • Do you see any errors – gkrthk Sep 02 '20 at 03:38
  • No, I don't receive any. – Dave Guerrero Sep 02 '20 at 03:39
  • Do you see the component html on inspect. Can you share the component code – gkrthk Sep 02 '20 at 03:43
  • When I do allocate the section to its own component and inspect if the component is there, it's not. – Dave Guerrero Sep 02 '20 at 03:46
  • Another quick check. Try moving the component out of the ngswitch and see if it loads – gkrthk Sep 02 '20 at 04:09
  • I tried that and it doesn't. – Dave Guerrero Sep 02 '20 at 04:09
  • 1
    Is MyAccountModelComponent listed in your `entryComponents`, and MyProfileComponent available in the same module? – Myk Willis Sep 02 '20 at 04:10
  • Does it load outside the modal? – gkrthk Sep 02 '20 at 04:10
  • MyAccountModalComponent is listed in my entry components, and yes, MyProfileComponent is listed within the same module. I added a console.log to MyProfileComponent to see if it's even being rendered, and it's not. The component does display outside the modal. – Dave Guerrero Sep 02 '20 at 04:20
  • I figured it out. I simply added an entryComponents array to the module I was working in and added the components to it. Before, all the components were listed in the app.module.ts. For some reason, I thought I already tested this method, so I didn't consider it a solution. Thank you for your guys help though. – Dave Guerrero Sep 02 '20 at 04:34
  • @DaveGuerro Do you know why entryComponents fixed it? I am having a similar issue but according to https://angular.io/guide/entry-components entryComponents is deprecated and everything should be taken care of by Ivy. – Jadamae77 Apr 07 '21 at 15:09

0 Answers0