Ben*_*Ben 7 dialog escaping angular-material angular
我有一个对话框表单,我想在用户按下转义键时优雅地关闭它。当用户按下转义键时,表单会立即关闭,但由于某种原因,对话框表单不会将结果发送到父表单。
通过取消按钮关闭时没有问题。
我已经在userform组件上使用“onKey”事件进行了尝试,但这也不起作用。
在我的打字稿和模板对话框文件中:
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
constructor(
private dialogRef: MatDialogRef<UpdateBonusConditieComponent>,
@Inject(MAT_DIALOG_DATA) private PassedData: ConditieTypeDialog,
) {} // I also tried it with a public PassedData instead of private
onCancel() {
console.log('komt ie hier');
this.PassedData.cancel = true;
}
onKey(event: any) {
this.onCancel();
console.log('toets ingedrukt ' + event.target);
}
onOK() {
console.log('OK button pressed');
}
Run Code Online (Sandbox Code Playgroud)
<mat-dialog-content [formGroup]="dialogConditieForm"
(keyup)="onKey($event)">
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button
color="accent"
(click)="onOK()"
[mat-dialog-close]="PassedData"
[disabled]="dialogConditieForm.invalid">Ok</button>
<button mat-button
color="warn"
(click)="onCancel()"
[mat-dialog-close]="PassedData">Cancel</button>
</mat-dialog-actions>
Run Code Online (Sandbox Code Playgroud)
然后我有我调用对话框的父表单,一些细节:
import { MatDialog } from '@angular/material';
constructor(private matDialog: MatDialog) {}
const dialogRef = this.matDialog.open(UpdateBonusConditieComponent, {
data: {
onderwerpGUI: onderwerpGUI,
isNewRow: isNewRow,
cancel: false,
}
});
dialogRef.afterClosed().subscribe((result: ConditieTypeDialog) => {
if (result.cancel) { //when hitting escape result is undefined for some reason
this.selectedRow = null;
return 0;
}
});
Run Code Online (Sandbox Code Playgroud)
我应该期待一个结果,以便this.selectedRow设置为,null但如果使用转义键关闭对话框表单,则不会发生这种情况。
我想我做错了什么。任何人都可以帮助我吗?
nas*_*h11 17
对话框表单没有发送结果,因为该onKey函数永远不会被调用。您可以改为订阅keyboardEventsinMatDialogRef并onCancel在Escape单击时调用。我还为backdropClick所需添加了相同的内容。
constructor(
private dialogRef: MatDialogRef<UpdateBonusConditieComponent>,
@Inject(MAT_DIALOG_DATA) private passedData: ConditieTypeDialog,
) { }
ngOnInit() {
this.dialogRef.keydownEvents().subscribe(event => {
if (event.key === "Escape") {
this.onCancel();
}
});
this.dialogRef.backdropClick().subscribe(event => {
this.onCancel();
});
}
onCancel(): void {
this.passedData.cancel = true;
this.dialogRef.close(this.passedData);
}
onOK() {
console.log('OK button pressed');
}
Run Code Online (Sandbox Code Playgroud)
此外,按照惯例,对变量名 ( passedData)使用驼峰式大小写。
| 归档时间: |
|
| 查看次数: |
4863 次 |
| 最近记录: |