如果在mat step之前打开任何文档,如何禁用/停止HostListener ?



在我的应用程序中,我使用Mat步进器。我正在收听关键字"Enter"并保存表单并重定向到下一步。我的问题是让我说在步骤4上我有两个按钮添加和编辑,让我说我点击编辑它打开文档的短窗口之前步骤4编辑表单,点击输入它保存数据并关闭文档查看器同时步进器也听输入关键字,然后进入下一步。如果打开任何文档,我想禁用HostListener

@HostListener('document:keydown', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
if (event.key == 'Enter') {
if (this.selectedStepper == '0') {
if (this.firstForm.valid) {
console.log('Stepper 1', this.fifthForm.valid)
this.onFirstSubmit()
this.stepper.next()
} else {
this.toastrService.danger('Incomplete Form', 'Message')
}
} else if (this.selectedStepper == '1') {
if (this.secondForm.valid) {
console.log('Stepper 2', this.secondForm.valid)
this.onSecondSubmit(this.country_birth)
this.stepper.next()
} else {
this.toastrService.danger('Incomplete Form', 'Message')
}
} else if (this.selectedStepper == '2') {
if (this.thirdForm.valid) {
console.log('Stepper 3', this.thirdForm.valid)
this.onThirdSubmit()
this.stepper.next()
} else {
this.toastrService.danger('Incomplete Form', 'Message')
}
} else if (this.selectedStepper == '3') {
if (this.fourthForm.valid) {
console.log('Stepper 4', this.fourthForm.valid)
this.onFourthSubmit()
// this.stepper.next();
} else {
this.toastrService.danger('Incomplete Form', 'Message')
}
} else if (this.selectedStepper == '4') {
if (this.fifthForm.valid) {
console.log('Stepper 5', this.fifthForm.valid)
this.onFifthSubmit()
this.stepper.next()
} else {
this.toastrService.danger('Incomplete Form', 'Message')
}
} else if (this.selectedStepper == '5') {
if (this.profileCompleteness == true) {
console.log('Stepper 6')
this.quickApply()
} else {
this.toastrService.danger('Incomplete Form', 'Message')
}
}
}
}
stepperCalled(event) {
this.selectedStepper = event.selectedIndex
}
<div class="Rtable-cell--heading">Add/Edit</div>
<div class="Rtable-cell--content date-content" *ngIf="showEdit == false && showOpenEdit == false">
<button nbButton *ngIf="addDegree" (click)="open(1)">Add</button>
<button nbButton *ngIf="editDegree" (click)="open(1)">Edit</button>
</div>
onFourthSubmit(){
console.log(this.fourthForm.valid);
var error = this.findInvalidControls();
// console.log(JSON.stringify(error))
if(this.fourthForm.valid){
this.stepper.next();
}
}
open(EducationalDialogNo) {
// SSC/CBSC dialogs validation commented as ssc not required
if (EducationalDialogNo == 1) {
console.log("EducationalDialogNo == 1");
this.dialogService.open(FirstDialogComponent).onClose.subscribe((data: any) => {
if (data !== undefined) {
this.cbse.university = data.sscUniversity;
this.cbse.school_name = data.sscCollege;
this.cbse.result_date = data.sscResultDate;
this.cbse.school_marks = data.sscMarks;
}
// this.stepper.stop();
err => console.error(err)
});
} else if (EducationalDialogNo == 2) {
//Code yrr
}
}

如何停用HostListener?对不起,我不知道,甚至不确定这是否可行,因为这是,在写作的时刻,一个功能请求

但是一个变通方法可以是:

  1. 创建一个变量(如果这些方法不在同一个组件中,请在服务中创建它)
  2. 控制,当keydown被触发时,如果你有一个对话框打开
  3. 如果是,不做任何处理
isDialogOpen = false
@HostListener('document:keydown', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
if(this.isDialogOpen) return
// Your code
}
open(EducationalDialogNo) {
// SSC/CBSC dialogs validation commented as ssc not required
if (EducationalDialogNo == 1) {
this.isDialogOpen = true
console.log('EducationalDialogNo == 1')
this.dialogService.open(FirstDialogComponent).onClose.subscribe((data: any) => {
this.isDialogOpen = false
if (data !== undefined) {
this.cbse.university = data.sscUniversity
this.cbse.school_name = data.sscCollege
this.cbse.result_date = data.sscResultDate
this.cbse.school_marks = data.sscMarks
}
// this.stepper.stop();
;(err) => console.error(err)
})
} else if (EducationalDialogNo == 2) {
//Code yrr
}
}

相关内容

  • 没有找到相关文章

最新更新