Angular material: is it possible to create modals (or dialogs) with ng-template?












2















In my project I am using ngx-bootstrap's dialog component which takes your ng-template and displays it as your modal.



Using ng-template is advantageous for many reasons and most importantly, there is no communication barrier (between modal and origin component) if ng-template live in same component. This way I can call my component method without any problems. For example, in following code selectNextRow() will change row in my table and hence selectedRow_Session and thus data of next row will be displayed on modal.



app.component.ts



/** display selectedRow_Session on modal */
<ng-template #sessionDetailTemplate>

<app-session-detail-model
[session]="selectedRow_Session"
[bot]="bot"
(selectNextRow)="selectNextRow()"
(closeModel$)="modalRef.hide()"
(selectPrevRow)="selectPrevRow()"
[pageNumberOfCurrentRowSelected]="pageNumberOfCurrentRowSelected"
[indexOfCurrentRowSelected]="indexOfCurrentRowSelected"
[finalDfState]="selectedRow_Session.df_state"
[sessionDataStore]="selectedRow_Session.data_store">
</app-session-detail-model>

</ng-template>


In Angular Material Dialogs, I could only find API to that can create modals with only Component and not with ng-template.



Is there a way to do this, with or without dialogs, using Angular Material?










share|improve this question




















  • 1





    The API doc shows that open() accepts a TemplateRef. So yes, it's possible.

    – JB Nizet
    Nov 25 '18 at 13:49











  • @JBNizet You are right! Thanks a lot!

    – i_read_terms_and_services
    Nov 25 '18 at 14:28
















2















In my project I am using ngx-bootstrap's dialog component which takes your ng-template and displays it as your modal.



Using ng-template is advantageous for many reasons and most importantly, there is no communication barrier (between modal and origin component) if ng-template live in same component. This way I can call my component method without any problems. For example, in following code selectNextRow() will change row in my table and hence selectedRow_Session and thus data of next row will be displayed on modal.



app.component.ts



/** display selectedRow_Session on modal */
<ng-template #sessionDetailTemplate>

<app-session-detail-model
[session]="selectedRow_Session"
[bot]="bot"
(selectNextRow)="selectNextRow()"
(closeModel$)="modalRef.hide()"
(selectPrevRow)="selectPrevRow()"
[pageNumberOfCurrentRowSelected]="pageNumberOfCurrentRowSelected"
[indexOfCurrentRowSelected]="indexOfCurrentRowSelected"
[finalDfState]="selectedRow_Session.df_state"
[sessionDataStore]="selectedRow_Session.data_store">
</app-session-detail-model>

</ng-template>


In Angular Material Dialogs, I could only find API to that can create modals with only Component and not with ng-template.



Is there a way to do this, with or without dialogs, using Angular Material?










share|improve this question




















  • 1





    The API doc shows that open() accepts a TemplateRef. So yes, it's possible.

    – JB Nizet
    Nov 25 '18 at 13:49











  • @JBNizet You are right! Thanks a lot!

    – i_read_terms_and_services
    Nov 25 '18 at 14:28














2












2








2


1






In my project I am using ngx-bootstrap's dialog component which takes your ng-template and displays it as your modal.



Using ng-template is advantageous for many reasons and most importantly, there is no communication barrier (between modal and origin component) if ng-template live in same component. This way I can call my component method without any problems. For example, in following code selectNextRow() will change row in my table and hence selectedRow_Session and thus data of next row will be displayed on modal.



app.component.ts



/** display selectedRow_Session on modal */
<ng-template #sessionDetailTemplate>

<app-session-detail-model
[session]="selectedRow_Session"
[bot]="bot"
(selectNextRow)="selectNextRow()"
(closeModel$)="modalRef.hide()"
(selectPrevRow)="selectPrevRow()"
[pageNumberOfCurrentRowSelected]="pageNumberOfCurrentRowSelected"
[indexOfCurrentRowSelected]="indexOfCurrentRowSelected"
[finalDfState]="selectedRow_Session.df_state"
[sessionDataStore]="selectedRow_Session.data_store">
</app-session-detail-model>

</ng-template>


In Angular Material Dialogs, I could only find API to that can create modals with only Component and not with ng-template.



Is there a way to do this, with or without dialogs, using Angular Material?










share|improve this question
















In my project I am using ngx-bootstrap's dialog component which takes your ng-template and displays it as your modal.



Using ng-template is advantageous for many reasons and most importantly, there is no communication barrier (between modal and origin component) if ng-template live in same component. This way I can call my component method without any problems. For example, in following code selectNextRow() will change row in my table and hence selectedRow_Session and thus data of next row will be displayed on modal.



app.component.ts



/** display selectedRow_Session on modal */
<ng-template #sessionDetailTemplate>

<app-session-detail-model
[session]="selectedRow_Session"
[bot]="bot"
(selectNextRow)="selectNextRow()"
(closeModel$)="modalRef.hide()"
(selectPrevRow)="selectPrevRow()"
[pageNumberOfCurrentRowSelected]="pageNumberOfCurrentRowSelected"
[indexOfCurrentRowSelected]="indexOfCurrentRowSelected"
[finalDfState]="selectedRow_Session.df_state"
[sessionDataStore]="selectedRow_Session.data_store">
</app-session-detail-model>

</ng-template>


In Angular Material Dialogs, I could only find API to that can create modals with only Component and not with ng-template.



Is there a way to do this, with or without dialogs, using Angular Material?







angular angular-material






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 13:42







i_read_terms_and_services

















asked Nov 25 '18 at 13:32









i_read_terms_and_servicesi_read_terms_and_services

11510




11510








  • 1





    The API doc shows that open() accepts a TemplateRef. So yes, it's possible.

    – JB Nizet
    Nov 25 '18 at 13:49











  • @JBNizet You are right! Thanks a lot!

    – i_read_terms_and_services
    Nov 25 '18 at 14:28














  • 1





    The API doc shows that open() accepts a TemplateRef. So yes, it's possible.

    – JB Nizet
    Nov 25 '18 at 13:49











  • @JBNizet You are right! Thanks a lot!

    – i_read_terms_and_services
    Nov 25 '18 at 14:28








1




1





The API doc shows that open() accepts a TemplateRef. So yes, it's possible.

– JB Nizet
Nov 25 '18 at 13:49





The API doc shows that open() accepts a TemplateRef. So yes, it's possible.

– JB Nizet
Nov 25 '18 at 13:49













@JBNizet You are right! Thanks a lot!

– i_read_terms_and_services
Nov 25 '18 at 14:28





@JBNizet You are right! Thanks a lot!

– i_read_terms_and_services
Nov 25 '18 at 14:28












1 Answer
1






active

oldest

votes


















2














As mentioned in comments, you can use a TemplateRef with @angular/material MatDialog. You can find the API reference here : Angular Material MatDialog.



Here is a minimal example showing how to do it :



import { Component, ViewChild, TemplateRef } from '@angular/core';
import { MatDialog } from '@angular/material';

@Component({
selector: 'dialog-overview-example',
template: `
<div [style.margin.px]="10">
<button mat-raised-button (click)="openDialog()">Open Modal via Component Controller</button>
</div>
<div [style.margin.px]="10">
<button mat-raised-button (click)="dialog.open(myTemplate)">Open Modal directly in template</button>
</div>

<ng-template #myTemplate>
<div>
<h1>This is a template</h1>
</div>
</ng-template>
`
})
export class DialogOverviewExample {
@ViewChild('myTemplate') customTemplate: TemplateRef<any>;

constructor(public dialog: MatDialog) {}

openDialog(): void {
const dialogRef = this.dialog.open(this.customTemplate, {
width: '250px'
});

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


And here is a live example using Angular v6 : Stackblitz Live Example.



Hope it helps!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467993%2fangular-material-is-it-possible-to-create-modals-or-dialogs-with-ng-template%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    As mentioned in comments, you can use a TemplateRef with @angular/material MatDialog. You can find the API reference here : Angular Material MatDialog.



    Here is a minimal example showing how to do it :



    import { Component, ViewChild, TemplateRef } from '@angular/core';
    import { MatDialog } from '@angular/material';

    @Component({
    selector: 'dialog-overview-example',
    template: `
    <div [style.margin.px]="10">
    <button mat-raised-button (click)="openDialog()">Open Modal via Component Controller</button>
    </div>
    <div [style.margin.px]="10">
    <button mat-raised-button (click)="dialog.open(myTemplate)">Open Modal directly in template</button>
    </div>

    <ng-template #myTemplate>
    <div>
    <h1>This is a template</h1>
    </div>
    </ng-template>
    `
    })
    export class DialogOverviewExample {
    @ViewChild('myTemplate') customTemplate: TemplateRef<any>;

    constructor(public dialog: MatDialog) {}

    openDialog(): void {
    const dialogRef = this.dialog.open(this.customTemplate, {
    width: '250px'
    });

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


    And here is a live example using Angular v6 : Stackblitz Live Example.



    Hope it helps!






    share|improve this answer




























      2














      As mentioned in comments, you can use a TemplateRef with @angular/material MatDialog. You can find the API reference here : Angular Material MatDialog.



      Here is a minimal example showing how to do it :



      import { Component, ViewChild, TemplateRef } from '@angular/core';
      import { MatDialog } from '@angular/material';

      @Component({
      selector: 'dialog-overview-example',
      template: `
      <div [style.margin.px]="10">
      <button mat-raised-button (click)="openDialog()">Open Modal via Component Controller</button>
      </div>
      <div [style.margin.px]="10">
      <button mat-raised-button (click)="dialog.open(myTemplate)">Open Modal directly in template</button>
      </div>

      <ng-template #myTemplate>
      <div>
      <h1>This is a template</h1>
      </div>
      </ng-template>
      `
      })
      export class DialogOverviewExample {
      @ViewChild('myTemplate') customTemplate: TemplateRef<any>;

      constructor(public dialog: MatDialog) {}

      openDialog(): void {
      const dialogRef = this.dialog.open(this.customTemplate, {
      width: '250px'
      });

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


      And here is a live example using Angular v6 : Stackblitz Live Example.



      Hope it helps!






      share|improve this answer


























        2












        2








        2







        As mentioned in comments, you can use a TemplateRef with @angular/material MatDialog. You can find the API reference here : Angular Material MatDialog.



        Here is a minimal example showing how to do it :



        import { Component, ViewChild, TemplateRef } from '@angular/core';
        import { MatDialog } from '@angular/material';

        @Component({
        selector: 'dialog-overview-example',
        template: `
        <div [style.margin.px]="10">
        <button mat-raised-button (click)="openDialog()">Open Modal via Component Controller</button>
        </div>
        <div [style.margin.px]="10">
        <button mat-raised-button (click)="dialog.open(myTemplate)">Open Modal directly in template</button>
        </div>

        <ng-template #myTemplate>
        <div>
        <h1>This is a template</h1>
        </div>
        </ng-template>
        `
        })
        export class DialogOverviewExample {
        @ViewChild('myTemplate') customTemplate: TemplateRef<any>;

        constructor(public dialog: MatDialog) {}

        openDialog(): void {
        const dialogRef = this.dialog.open(this.customTemplate, {
        width: '250px'
        });

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


        And here is a live example using Angular v6 : Stackblitz Live Example.



        Hope it helps!






        share|improve this answer













        As mentioned in comments, you can use a TemplateRef with @angular/material MatDialog. You can find the API reference here : Angular Material MatDialog.



        Here is a minimal example showing how to do it :



        import { Component, ViewChild, TemplateRef } from '@angular/core';
        import { MatDialog } from '@angular/material';

        @Component({
        selector: 'dialog-overview-example',
        template: `
        <div [style.margin.px]="10">
        <button mat-raised-button (click)="openDialog()">Open Modal via Component Controller</button>
        </div>
        <div [style.margin.px]="10">
        <button mat-raised-button (click)="dialog.open(myTemplate)">Open Modal directly in template</button>
        </div>

        <ng-template #myTemplate>
        <div>
        <h1>This is a template</h1>
        </div>
        </ng-template>
        `
        })
        export class DialogOverviewExample {
        @ViewChild('myTemplate') customTemplate: TemplateRef<any>;

        constructor(public dialog: MatDialog) {}

        openDialog(): void {
        const dialogRef = this.dialog.open(this.customTemplate, {
        width: '250px'
        });

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


        And here is a live example using Angular v6 : Stackblitz Live Example.



        Hope it helps!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 14:34









        GeoAstronauteGeoAstronaute

        82627




        82627






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467993%2fangular-material-is-it-possible-to-create-modals-or-dialogs-with-ng-template%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)