Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

864
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda