Repository navigation
Open
Labels
P4A relatively minor issue that is not relevant to core functionsarea: material/dialogfeatureLabel used to distinguish feature request from other issues
Description
Bug, feature request, or proposal:
feature request
What is the expected behavior?
I'd like to use something like this (just like mat-menu):
<button (click)="myDialog.open()">Open it</button>
<mat-dialog (result)="onResult($event)" #myDialog>
<h1 mat-dialog-title>Dialog Title</h1>
<mat-dialog-content>
Content
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button [mat-dialog-close]="false">Cancel</button>
<button mat-button [mat-dialog-close]="true">Confirm</button>
</mat-dialog-actions>
</mat-dialog>
What is the current behavior?
Doesn't exist as fat as I have seen in the docs and source code.
What are the steps to reproduce?
I've created a small component which demonstrates what I'm looking for:
https://stackblitz.com/edit/angular-material2-issue-tp5aej?file=app%2Fapp.component.html
(I didn't get mat-dialog-close working with the projection, though I chose to just add a method to my dialog component)
What is the use-case or motivation for changing an existing behavior?
There are many use cases where I just want a very simple dialog, and don't want to write 30 lines of code.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular 6 beta
Material 6 beta
Reacted by GerbenVersluis, Markus Thiel, Tobias Gassmann, Arun Teja Dhondi, Pedro De almeida, Aleksei Lebedev, Romain Ciaccafava, gak10100, Giora Guttsait, Cyrus Frost and 1 more
Reacted by Edoardo Luppi
Metadata
Metadata
Assignees
Labels
P4A relatively minor issue that is not relevant to core functionsarea: material/dialogfeatureLabel used to distinguish feature request from other issues
Activity
EdricChan03 commentedon Mar 19, 2018
This can be done using
<ng-template>:app.component.html:app.component.ts:Demo
See the code in the snippet below for more info:
components/src/lib/dialog/dialog.ts
Lines 105 to 113 in c28549d
benneq commentedon Mar 19, 2018
Thank you.
But your solution doesn't support the Dialog results. Using
dialogRef.afterClosed().subscribe(...)will make the code even larger, and I guess it's not that easy to say#myDialog1should callonFirstResult($event)and#myDialog2should callonSecondResult($event).kimamula commentedon Oct 20, 2018
I would like to add that if you need
MatDialogRefinstance in your dialog component in the<ng-template>pattern @Chan4077 suggested above, it's as easy as:I like this usage and I think it would be nice if the usage is clearly documented.
j2L4e commentedon Mar 23, 2019
This has been solved in #9379 even before this issue came up.
A naive wrapper component could look like this
Use it like this:
caveat:
both
DialogComponentandMatDialogClosehave to be available in the template. A missing directive fails silently, so make sure to reexportMatDialogCloseor importMatDialogModulealmeidap commentedon Mar 29, 2019
@benneq Thanks for your solution for a temporary
MatDialogimplementation!@j2L4e It looks like that
[mat-dialog-close]inside a dialogTemplateReffails even after exportingMatDialogCloseand/or importingMatDialogModulein the closest parent module:arlowhite commentedon May 14, 2019
IMO, this is a documentation issue. https://material.angular.io/components/dialog/overview needs an example with TemplateRef using implicit context. This is difficult to figure out if you're not comfortable looking at material2's source code.
The
$implicitcontext is bound toMatDialogConfig.data, which I don't think most people know about. So in this code example,foo, which is arbitrary, is bound to$implicit, which is fromMatDialogConfig.data, see material2/src/lib/dialog/dialog.ts_attachDialogContent()Then, you need to bind TemplateRef in your component, or just pass it by reference when calling a function from the template.
Component .ts
tonyuifalean commentedon Mar 4, 2020
@arlowhite, didn't you get the error:
ERROR Error: No component factory found for undefined. Did you add it to @NgModule.entryComponents?
?
EdricChan03 commentedon Mar 4, 2020
@tonyuifalean Could you show us your code?
If you're facing an unrelated issue, I suggest that you should either try to ask on StackOverflow or open a new issue on this repository.
tonyuifalean commentedon Mar 4, 2020
@EdricChan03: I am using Angular 8, and I am just testing @arlowhite's example.

In my template I have:
In the component I have a method, called in ngOnInit:
@ViewChild('myTemp', { static: false })
public myTemp: TemplateRef;
public openMyDialog() {
this.dialog.open(this.myTemp);
}
And in the module I import: MatDialogModule
EdricChan03 commentedon Mar 4, 2020
Could you try moving the
public myTempline to the same line as theViewChild(...)line? And is there any reason why you're marking all of the methods and properties aspublic?EdricChan03 commentedon Mar 4, 2020
@arlowhite Actually, if you look closely in the documentation forTemplateRef, passing in the data interface as theTtype parameter will not make any difference than passing inanyasTemplateRefis defined to have aCtype parameter which is the template's class.So here's the modified version of your component code:Oops, never mind. I did not read the bottom part that mentioned what the
Ctype parameter is for:tonyuifalean commentedon Mar 4, 2020
Just for testing purposes. In the end I'll try to use a component directly, but thanks for you support!
angular-robot commentedon Feb 1, 2022
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.
Find more details about Angular's feature request process in our documentation.
angular-robot commentedon Feb 22, 2022
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.
We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.
You can find more details about the feature request process in our documentation.