如何在我的angular 7应用中添加一个简单对话框?

时间:2018-10-11 07:57:11

标签: angular dialog angular7

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

我的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方法中加载对话框。最终,我将对对话框做更多的事情,但是现在仅加载对话框就足够了。

谢谢。

1 个答案:

答案 0 :(得分:1)

要打开对话框:

  • 确保将MatDialog注入您的班级
  • 使用注入的对话框,运行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 {
}