Skip to content

when MatDialog open a TemplateRef, can support to pass a context? #16665

@rorry121

Description

@rorry121

Feature Description

when MatDialog open a TemplateRef, how to pass data to a template, can support to pass a context?

Use Case

code:

  dialogRef: MatDialogRef<any>;
  list = [{
    name: 'banana',
    parent_id: 'fruit'
  },{
    name: 'pumpkin',
    parent_id: 'vegetables'
  },{
    name: 'monkey',
    parent_id: 'Animal'
  }];
  @ViewChild('dialogTemplate') dialogTemplate: TemplateRef<any>;

  constructor(
    private matDialog: MatDialog,
  ) {
  }

  show(item) {
    //   how to pass the item to this.dialogTemplate ?
    this.dialogRef = this.matDialog.open(this.dialogTemplate);
  }
<ul>
  <li *ngFor="let item of list" (click)="show(item)"></li>
</ul>

<ng-template #dialogTemplate let-item>
  <section class="confirm-dialog">
    <article mat-dialog-title class="mat-dialog-title">
      <span class="text">Tips</span>
      <button mat-dialog-close class="no-padding">
        <i class="dialog-close ico-delete"></i>
      </button>
    </article>
    <article mat-dialog-content>
      <div>I like {{item.name}}</div>
    </article>
    <article mat-dialog-actions>
      <button mat-button class="mat-button" >confirm</button>
      <button mat-button class="mat-button" mat-dialog-close >cancel</button>
    </article>
  </section>
</ng-template>

Now I want to open a Dialog, and pass the item (I clicked ) as a context to the dialogTemplate.

overlayRef.attach(new TemplatePortal(templateRef, viewContainerRef, context));

Overlay can attach a TemplatePortal that maintains a context, why the matDialog not support this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureThis issue represents a new feature or feature request rather than a bug or bug fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions