Home > Mobile >  How to Disable/Stop HostListener if any document is opened before mat stepper?
How to Disable/Stop HostListener if any document is opened before mat stepper?

Time:10-25

In my Application i am using Mat stepper. I am Listening Keyword 'Enter' and saving the form and redirecting to next step. My ques is let say on stepper 4 i have two buttons Add and edit let say i clicked edit it opens Short window of document before stepper 4 to edit form and on click enter it saves the data and close the document viewer same time stepper also listens the enter Keyword and goes to next step. I want to disable HostListener if any document is opened.

@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
  }
 }

CodePudding user response:

How to deactivate the HostListener? Sorry I don't know, not even sure if this is doable since this is, at the moment of writing, a feature request

But a workaround could be :

  1. Creating a variable (if those method aren't in the same component, do create it in a service).
  2. Controlling, when the keydown get triggered, if you have a dialog open
  3. If yes, do nothing
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
  }
}
  • Related