Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

860
Visualizações
How to disable right click on whole website (not just a few pages) in Angular

I have tried to search for solutions which would allow me to disable right click on the whole application in Angular 2+ but I get only solutions for disabling on few components like,

<someSelector appDisableRightClick></someSelector>

where, someSelector is any element of HTML, appDisableRightClick is a directive to disable right click using hostlistener for contextmenu event.

The problem with this is it will have to be written everywhere which means if I want to disable right click everywhere in my application, this is not something I should be looking for. Need a little help here please for the optimum solution to achieve this for the whole application.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Working demo in this Stackblitz Demo Link

Basically need to access document object inside angular service using injection token of document, so that its safe way to access dom using document inside angular. below is service code

export class DisableRightClickService {
  constructor(@Inject(DOCUMENT) private document: Document) {}
  disableRightClick() {
    this.document.addEventListener('contextmenu', (event) =>
      event.preventDefault()
    );
  }
}

In above code we are adding contextmenu event to document and at the same time we prevent event to occur too!. Now, need to add this inside app.component.ts file like below..

export class AppComponent {
 constructor(private rightClickDisable: DisableRightClickService) {}
 ngOnInit() {
   this.rightClickDisable.disableRightClick();
 } 
} 

This way you can disable right click for your whole application.

over 4 years ago · Santiago Trujillo Relatório

0

As commented by @GRD, the below link provides a good and acceptable solution for the problem. Asked him to put comment in answers section, but he didn't, need to close this question, so putting it myself here.

https://stackblitz.com/edit/angular-ivy-pvmcrz?file=src%2Fapp%2Fapp.component.ts

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda