-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dialog with inline Template #10459
Comments
This can be done using
<button mat-button (click)="openDialogWithRef(myDialog)">Open dialog with template ref</button>
<!-- or... -->
<button mat-button (click)="openDialogWithoutRef()">Open dialog without template ref</button>
<ng-template #myDialog>
<h2 matDialogTitle>Dialog!</h2>
<mat-dialog-content>
<p>Dialog content goes here</p>
</mat-dialog-content>
<mat-dialog-actions align="end">
<button mat-button matDialogClose color="primary">DISMISS</button>
</mat-dialog-actions>
</ng-template>
<ng-template #secondDialog>
<h2 matDialogTitle>Such dialog very wow!</h2>
<mat-dialog-content>
<p><em>*insert meme here*</em></p>
</mat-dialog-content>
<mat-dialog-actions align="end">
<button mat-button matDialogClose color="primary">Shutdown dialog</button>
</mat-dialog-actions>
</ng-template>
import { ViewChild, TemplateRef } from '@angular/core';
import { MatDialog } from '@angular/material';
// ...
export class AppComponent {
@ViewChild('secondDialog') secondDialog: TemplateRef<any>;
constructor(private dialog: MatDialog) { }
openDialogWithRef(templateRef: TemplateRef<any>) {
this.dialog.open(templateRef);
}
openDialogWithoutRef() {
this.dialog.open(this.secondDialog);
}
} Demo components/src/lib/dialog/dialog.ts Lines 105 to 113 in c28549d
|
Thank you. But your solution doesn't support the Dialog results. Using |
I would like to add that if you need <ng-template #myDialog let-dialogRef="dialogRef">
<my-dialog [dialogRef]="dialogRef"></my-dialog>
</ng-template> I like this usage and I think it would be nice if the usage is clearly documented. |
This has been solved in #9379 even before this issue came up. import { ChangeDetectionStrategy, Component, ContentChild, EventEmitter, Output, TemplateRef } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material';
@Component({
selector: 'app-dialog',
templateUrl: './dialog.component.html',
styleUrls: ['./dialog.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DialogComponent {
@ContentChild(TemplateRef) template: TemplateRef<{}>;
@Output() result = new EventEmitter();
dRef: MatDialogRef<any>;
constructor (private dialog: MatDialog) {}
open () {
this.dRef = this.dialog.open(this.template);
this.dRef.afterClosed().subscribe(val => this.result.emit(val));
}
} Use it like this: <button (click)="d.open()">click me</button>
<app-dialog #d (result)="onResult($event)">
<ng-template>
<button [mat-dialog-close]="someProperty">leave me alone!</button>
</ng-template>
</app-dialog> caveat: |
@benneq Thanks for your solution for a temporary @j2L4e It looks like that jit_nodeValue_3(...).dialogRef.close is not a function
at Object.eval [as handleEvent] (TestComponent.html:17)
at handleEvent (core.js:23009)
at callWithDebugContext (core.js:24079)
at Object.debugHandleEvent [as handleEvent] (core.js:23806)
at dispatchEvent (core.js:20458)
at core.js:20905
at HTMLButtonElement.<anonymous> (platform-browser.js:993) |
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 <ng-template #myTemp let-foo>
<h2 matDialogTitle>My Title</h2>
<mat-dialog-content>
<p>{{ foo.myProperty }}</p>
</mat-dialog-content>
</ng-template> Then, you need to bind TemplateRef in your component, or just pass it by reference when calling a function from the template. <button mat-stroked-button
(click)="openDialog(myTemp, myData)">
Open Dialog
</button> Component .ts openDialog(templateRef: TemplateRef<MyData>, myData: MyData) {
this.dialog.open(templateRef, { data: myData });
} |
@arlowhite, didn't you get the error: |
@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. |
@EdricChan03: I am using Angular 8, and I am just testing @arlowhite's example. In the component I have a method, called in ngOnInit: And in the module I import: MatDialogModule |
Could you try moving the |
openDialog(templateRef: TemplateRef<any>, myData: MyData) {
this.dialog.open(templateRef, { data: myData });
} Oops, never mind. I did not read the bottom part that mentioned what the
|
Just for testing purposes. In the end I'll try to use a component directly, but thanks for you support! |
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. |
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. |
Bug, feature request, or proposal:
feature request
What is the expected behavior?
I'd like to use something like this (just like
mat-menu
):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
The text was updated successfully, but these errors were encountered: