[英]How to use the function in the modal window in Angular
我有一個簡單的文件列表和“刪除”按鈕。 我添加了模態窗口進行確認。但是,我不知道如何在模態窗口中添加主要組件中的Delete函數。對於模態窗口Im使用庫@ 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();
}
}
您可以使用回調函數
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);
})
}
}
和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();
}
}
將此添加到您的模式模板:
...
<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);
})
}); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.