如何向我的 angular 7 应用程序添加一个简单的对话框?

How do I add a simple dialog box to my angular 7 app?

我在 angular 7 中有一个应用程序,我从 api 中提取数据,将数据加载到 table 中。 table 的末尾有一列带有一个按钮。单击该按钮会调用一个函数,该函数只会将行数据记录到控制台。 这是我的代码:

我的 component.ts 文件:

import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { MatTableDataSource } from "@angular/material";
import { Object} from "../object.model";
import {MatDialog, MatDialogRef} from '@angular/material';

@Component({
   styleUrls: ["./styles.scss"],
   templateUrl: "./template.html"
})
export class MyRouteData implements OnInit {
   employeeInfoTable : Object[] = [];
   employeeInfoTableDataSource = new MatTableDataSource(this.employeeInfoTable);
   displayedColumns: string[] = [
     "Name",
     "DateOfBirth",
     "Address",
     "Postcode",
     "Gender",
     "Salary"
     "JobTitle"
     "AdditionalDetails"
  ];

   constructor(private http: HttpClient) {}

   ngOnInit() {
      this.http.get("http://localhost:5000/MyRoute/GetEmployeeInfo")
          .subscribe(response => {
             this.employeeInfoTable = response;
             this.employeeInfoTableDataSource.data = this.employeeInfoTable;
      });
   }

   displayAdditionalDetails(row) {
    console.log("Row sent from link", row);

  }
}

export interface Object{
   id: number;
   Name: string;
   DateOfBirth: Date;
   Address: string;
   Postcode: string;
   Gender: string;
   Salary : number;
   JobTitle : string;
}

我的template.html文件:

<mat-card style="height: 98%">
  <table mat-table [dataSource]="employeeInfoTableDataSource" class="mat-elevation-z8">
    <ng-container matColumnDef="Name">
        <th mat-header-cell *matHeaderCellDef>Name </th>
        <td mat-cell *matCellDef="let element"> {{element.Name}} </td>
    </ng-container>
    <ng-container matColumnDef="DateOfBirth">
        <th mat-header-cell *matHeaderCellDef> Date Of Birth </th>
        <td mat-cell *matCellDef="let element"> {{element.DateOfBirth}} </td>
    </ng-container>
    <ng-container matColumnDef="Address">
        <th mat-header-cell *matHeaderCellDef> Address </th>
        <td mat-cell *matCellDef="let element"> {{element.Address}} </td>
    </ng-container>
    <ng-container matColumnDef="Postcode">
        <th mat-header-cell *matHeaderCellDef> Postcode </th>
        <td mat-cell *matCellDef="let element"> {{element.Postcode}} </td>

    <ng-container matColumnDef="Gender">
        <th mat-header-cell *matHeaderCellDef> Gender </th>
        <td mat-cell *matCellDef="let element"> {{element.Gender}} </td>
    </ng-container>
    <ng-container matColumnDef="Salary">
        <th mat-header-cell *matHeaderCellDef> Salary </th>
        <td mat-cell *matCellDef="let element"> {{element.Salary}} </td>
    </ng-container>
    <ng-container matColumnDef="JobTitle">
        <th mat-header-cell *matHeaderCellDef> Job Title </th>
        <td mat-cell *matCellDef="let element"> {{element.JobTitle}} </td>
    </ng-container>
    <ng-container matColumnDef="AdditionalDetails">
        <th mat-header-cell *matHeaderCellDef> AdditionalDetails </th>
        <td mat-cell *matCellDef="let element">  <button mat-icon-button color="warn" (click)="displayAdditionalDetails(element)">
          <mat-icon aria-label="Example icon-button with a heart icon">description</mat-icon>
        </button></td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
</mat-card>

我现在要做的是在选择按钮时加载一个对话框。

我添加了一个名为 dialog-overview-example-dialog.html 的新 html 文件,其中包含以下内容:

<div mat-dialog-content>
    <p>“I find your lack of faith disturbing.” — Darth Vader</p>

</div>

并且我已将以下内容添加到我的 component.ts 文件的末尾,该文件是我从某处的某个教程中获得的:

@Component({
  selector: "dialog-overview-example-dialog",
  templateUrl: "dialog-overview-example-dialog.html"
})
export class DialogOverviewExampleDialog {
  constructor(public dialogRef: MatDialogRef<DialogOverviewExampleDialog>) {}

  onCloseClick(): void {
    this.dialogRef.close();
  }
}
@Component({
  selector: 'dialog-overview-example',
  templateUrl: 'dialog-overview-example.html',
})

export class DialogOverviewExample {

  constructor(public dialog: MatDialog) {}

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

}

我现在需要做的就是弄清楚如何从 displayAdditionalDetails 方法加载对话框。最终我会用对话框做更多的事情,但现在只加载对话框就足够了。

谢谢。

打开对话框:

  • 确保 MatDialog 已注入您的 class
  • 使用注入的对话框,运行 dialog.open(component, options)
    • component就是你要在对话框中渲染的内容
    • options 允许调整框,并将数据传递到对话框

示例:

@Component({
  styleUrls: ["./styles.scss"],
  templateUrl: "./template.html"
})
export class MyRouteData implements OnInit {

  constructor(private dialog: MatDialog) {}

  displayAdditionalDetails(row) {
    this.dialog.open(DialogOverviewExampleDialog, {
      width: '500px',
      data: row,
    })
  }
}

这将为您打开对话框。注意选项中的 data: row。这就是将数据传递到对话框的方式。

如果您想在对话框中使用数据,您需要将 MAT_DIALOG_DATA 注入到您的对话框组件中,如下所示:

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog implements OnInit {

  constructor(
    private dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) private data: any)
  { }

  ngOnInit() {
    console.log(this.data); // this is the "row" passed in
  }

  onCloseClick(): void {
    this.dialogRef.close();
  }

}

如果您正在使用 AOT 编译,您还需要将呈现为对话框的任何组件添加到 entrycomponents:

下的模块中
@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  entryComponents: [DialogOverviewExampleDialog],
  exports: [
    ...
  ],
})
export class MyModule {
}