Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular material create dialog box in same component as existing functionality

I am trying to add an Angular Material dialog box(just a title and yes/no) that gets called before my web service executes. The thing is that I do not want to create the dialogs HTML in a separate component. I need the dialogs HTML to be in the same file as my existing code. The dialog needs to open when I click on the callAPI button. Here is my existing component code

<mat-tab-group>
    <mat-tab label="Tab 1">
       <button mat-flat-button color="warn" (click)="callAPI()">Open Dialog</button>
    </mat-tab>
    <mat-tab label="Tab 2">
    </mat-tab>
</mat-tab-group>
callAPI() {
    this.http.get<any>('https://example.com/api').subscribe(data => {
      this.data = data;
      this.loading = false;
    },
    err => {
        this.loading = false;
    });
}
like image 885
skydev Avatar asked Sep 13 '18 09:09

skydev


People also ask

How do you pass data in angular material dialog?

We can pass data to the dialog component by using the data property of the dialog configuration object. As we can see, the whole data object initially passed as part of the dialog configuration object can now be directly injected into the constructor.

How do you align a mat button to the right?

To right align the mat dialog actions buttons, use align="end" attribute on mat-dialog-actions container.

How do I import MatDialogModule?

In order to use the dialog component, we need to import the MatDialogModule module into the app module like this. import { MatDialogModule } from '@angular/material/dialog'; After importing the dialog module, the next step is to include it inside the imports array as given below.


1 Answers

Update: I was not correct in my assumptions that the TemplateRef's type parameter was the component reference - in fact, it's actually the "data-binding context of the embedded view", as shown in the documentation for the TemplateRef#createEmbeddedView method:

abstract createEmbeddedView(context: C): EmbeddedViewRef<C>

Description:

Instantiates an embedded view based on this template, and attaches it to the view container.

Parameters:

context (type: C) The data-binding context of the embedded view, as declared in the usage.


You can pass in a template reference to MatDialog#open:

<ng-template #callAPIDialog>
    <h2 matDialogTitle>Hello dialog!</h2>
    <mat-dialog-actions align="end">
        <button mat-button matDialogClose="no">No</button>
        <button mat-button matDialogClose="yes">Yes</button>
    </mat-dialog-actions>
</ng-template>
import { TemplateRef, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material';

@Component({ /* */ })
export class MyComponent {

    @ViewChild('callAPIDialog') callAPIDialog: TemplateRef<any>;

    constructor(private dialog: MatDialog) { }

    callAPI() {
        let dialogRef = this.dialog.open(this.callAPIDialog);
        dialogRef.afterClosed().subscribe(result => {
            // Note: If the user clicks outside the dialog or presses the escape key, there'll be no result
            if (result !== undefined) {
                if (result === 'yes') {
                    // TODO: Replace the following line with your code.
                    console.log('User clicked yes.');
                } else if (result === 'no') {
                    // TODO: Replace the following line with your code.
                    console.log('User clicked no.');
                }
            }
        })
    }
like image 88
Edric Avatar answered Sep 23 '22 06:09

Edric