首页 > 解决方案 > 如何在Angular的模态窗口中使用该功能

问题描述

我有一个简单的文件列表和“删除”按钮。我添加了模态窗口以进行确认。但是,我不知道如何将主组件中的删除功能添加到模态窗口。对于模态窗口,我使用库 @angular/material。我的目标是通过在模态窗口中单击带有 class=accept() 的按钮来删除文件。

export class FileService {

constructor(private http: HttpClient, @Inject('BASE_URL') 
baseUrl: string, public dialog: MatDialog ) {}

public openDeleteModal(name: any, id: any) {
this.dialog.open(DeleteDialog, { data: { name , id} }); 
}

public fileDelete(id) {
return this.http.delete(this.Url + '/delete' + id).subscribe(
  data => {
    console.log("DELETE Request is successful ", data);
  },
  error => {
    console.log("Error", error);
  })
 }
 }

@Component({
selector: 'list-files-deletedialog',
templateUrl: './list-files-deletedialog.component.html',
  })
export class DeleteDialog {
constructor(public dialogRef: MatDialogRef<DeleteDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) { }

 public accept(): void {

  // here i want to implement function fileDelete
 }

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

标签: angulartypescript

解决方案


在你的模态模板中添加这个:

...

<mat-dialog-actions align="end">
  <button mat-button mat-dialog-close>Cancel</button>
  <button mat-button [mat-dialog-close]="data" cdkFocusInitial>DELETE</button>

...

订阅数据

 openDialog(data) {
        const dialogRef = this.dialog.open(DialogContentExampleDialog);

        dialogRef.afterClosed().subscribe(data=> {
          return this.http.delete(this.Url + '/delete' + id).subscribe(
          data => {
          console.log("DELETE Request is successful ", data);
  },
  error => {
    console.log("Error", error);
  })

}); }


推荐阅读