我有一个简单的文件列表和“删除”按钮。我添加了模态窗口进行确认。但是,我不知道如何在模态窗口中添加主要组件中的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();
}
}
答案 0 :(得分:0)
您可以使用回调函数
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();
}
}
答案 1 :(得分:0)
将此添加到您的模式模板中
FunctionTable
订阅数据
...
<mat-dialog-actions align="end">
<button mat-button mat-dialog-close>Cancel</button>
<button mat-button [mat-dialog-close]="data" cdkFocusInitial>DELETE</button>
...
}); }