简体   繁体   中英

How to use the function in the modal window in Angular

I have a simply list of files and button "Delete". I added modal window for confirmation.But, i dont know how to add my Delete function that is in main component to the modal window.For modal window Im using library @angular/material. My goal is delete file by clicking button with class=accept() in modal window.

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();
 }
}

You can use the callback function

export class FileService {

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

public openDeleteModal(name: any, id: any, cb?: any) {
   let deleteModelRef: MatDialogRef<DeleteDialog>;
   this.dialog.open(DeleteDialog, { data: { name , id} }); 

   deleteModelRef.afterClosed().subscribe(result => {
        if (cb) {
            cb();
        }
   });
}

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);
    })
   }
}

and DeleteDialogue.component

@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, private fileService: FileService) { }

 public accept(): void {

  this.fileservice.openDeleteModal('name',id, cb => {
     // here you can call delete service like
     this.fileservice.fileDelete(id);
  });
 }

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

add this at your modal template:

...

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

...

subscribe for data

 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);
  })

}); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM