12
<div class="container" (contextmenu)="onRightClick()">
</div>

I want to disable right click in a few components and not the whole website. I have to define the below function in all the components where I want to disable right click. What's the best way to do it so that I don't have to define the function again and again in those components

onRightClick() {
  return false;
}
Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
zuyi
  • 459
  • 2
  • 8
  • 17

7 Answers7

32

It's the contextmenu event : create a directive to manage that.

Stackblitz

@HostListener('contextmenu', ['$event'])
onRightClick(event) {
  event.preventDefault();
}
  • returning `false` from the method will also do like `onRightClick(event) { return false; }`. So, if possible can you suggest which one is better. – Abhishek Kumar Jun 19 '19 at 06:59
  • @AbhishekKumar [look for it on google](https://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false) –  Jun 19 '19 at 07:30
14

My advice is to make a directive that will have click binded to element where it is attached. You got small tutorial on their official documentation and it should give you clear path on what you should make.

In this case, you can reuse directive all over your app and will do just the same thing. Will bind click to element where it is attached and will have some logic in it, so you don't repeat yourself.

doc

Example:

import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[appDisableRightClick]'
})
export class DisableRightClickDirective {
  @HostListener('contextmenu', ['$event'])
  onRightClick(event) {
    event.preventDefault();
  }
}

you can also make module that will export this directive and in order to use it in your other modules, just import that module. Modules should look like the following:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DisableRightClickDirective } from './disable-right-click.directive';

@NgModule({
  declarations: [
    DisableRightClickDirective
  ],
  imports: [
    CommonModule
  ],
  exports: [
    DisableRightClickDirective
  ]
})
export class DisableRightClickModule {
}

In every module, where you'd like to use this directive, all you have to do is import DisableRightClickModule. And that's pretty much it.

import { NgModule } from '@angular/core';
import { DisableRightClickModule } from './disable-right-click.module';

@NgModule({
  declarations: [
   ...
  ],
  imports: [
    ....,
    DisableRightClickModule
  ]
})
export class SomeDummyModule {
}

Inside your HTML just attach directive to any element as attribute eg.

<a href="#" appDisableRightClick>
Nikola Stekovic
  • 635
  • 3
  • 13
5

you can easily do like below:

<div class="container" (contextmenu)="onRightClick()"></div>
onRightClick(event) {
  event.preventDefault();
}

note: this code only disable right click for the div you referred. If you want to disable for all page you can use:

@HostListener('contextmenu', ['$event'])
  onRightClick(event) {
  event.preventDefault();
}
ssuperczynski
  • 3,190
  • 3
  • 44
  • 61
Me Sa
  • 1,036
  • 12
  • 14
  • This answer fits for my situation, I only need to disable right-click on few components. If you need to disable in the whole project, create a directive. – alexchvrches Mar 26 '20 at 21:50
3

If you are interested to disable right click on entire Angular app, use below in index.html file.
<body class="mat-typography" oncontextmenu="return false">

Palash Roy
  • 1,547
  • 1
  • 15
  • 11
1

You have to use a script to disable right click & F12 functions. Add below script in index.html that is in root path in your angular application.

First, add oncontextmenu="return false;" as a HTML attribute for <body> tag as below.

<body oncontextmenu="return false;">

Your <app-root></app-root> goes next to the <body> tag*

Then, add below script before close <body> tag.

<script>document.addEventListener("contextmenu",e=>e.preventDefault()),document.onkeydown=function(e){return 123!=e.keyCode&&((!e.ctrlKey||!e.shiftKey||73!=e.keyCode)&&((!e.ctrlKey||!e.shiftKey||74!=e.keyCode)&&((!e.ctrlKey||85!=e.keyCode)&&void 0)))};</script>

Finaly, close </body> tag & serve your application again.

1

Can someone explain why a directive solution is better then simple saying oncontextmenu="return false"

@HostListener('contextmenu', ['$event'])
  onRightClick(event) {
  event.preventDefault();
}

With a directive you would have to load the directive anytime you are using it to a specific module. I could see that a directive makes sense if you are doing things other than simply disabling the contextmenu. I would like to understand why the directive implementation is more effective than simply saying contextmenu="return false;" mycontextmenudirective vs contextmenu="return false;"

vou xiong
  • 11
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34098664) – Klaassiek Mar 30 '23 at 10:50
0

It worked for me without Hostistner. (contextmenu)="$event?.preventDefault();"

Zia Khan
  • 188
  • 2
  • 9