0

I have a button, when clicked goes to the next route/page in Angular. Is there a way we can use the same button but instead of clicking it sometimes give the user to enter a secret passphrase on the keyboard to navigate to the next page. Example: if a user types "nextpage" he should go to the next page.

Here is the working stackblitz code for page routing

HTML:

<button (click)=goToNextPage()>Click me or type the passphrase</button>

TS:

goToNextPage() {
    this.router.navigate(['/newPage'], { skipLocationChange: true });
  }

1 Answer 1

1
constructor() {
  document.addEventListener('keyup', this.addkey);
}

typed = '';

addkey = event =>  {
  this.typed += String.fromCharCode(event.keyCode);
  if (this.typed.endsWith('NEXTPAGE')) {
    this.goToNextPage();
  }
};

ngOnDestroy() {
  document.removeEventListener('keyup', this.addkey);
}

https://stackblitz.com/edit/us-keyboard-to-go-7kmsl7?file=src/app/checkin/checkin.component.ts

You could put in a test for backspace to remove the last character if you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.