Tuesday 19 December 2023

How to Turn off Angular's Right Click Directive?

Leave a Comment

This post will explain how to make a custom directive in Angular that will prevent right-clicking.



Required conditions
Basic familiarity with Angular 2 or later; installation of Node and NPM; Visual Studio Code; Bootstrap (Optional)

Make an Angular Project
The command to create an Angular project is as follows:
ng new angapp

Now install Bootstrap by using the following command,
npm install bootstrap --save

Now open the styles.css file and add Bootstrap file reference. To add a reference in the styles.css file add this line.
@import '~bootstrap/dist/css/bootstrap.min.css';

Create Directive
Create  a new directive using the Angular CLI command
ng generate directive rightclick

Now open rightclick.directive.ts file and add following code
import { Directive, HostListener, Input, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';
  @Directive({
    selector: '[DisableRightClick]'
  })
  export class DisableRightClickDirective {
    constructor() {}

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


Now open app.component.html file and add the following code.

<div class="container" style="margin-top:10px;margin-bottom: 24px;">
  <div class="col-sm-12 btn btn-info">
    Disable Right Click Directive in Angular Application
  </div>
</div>
<div class="container" DisableRightClick>
  Right click is disabled on this element
</div>


Now open app.module.ts and following code
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DisableRightClickDirective } from './rightclick.directive';


@NgModule({
  declarations: [
    AppComponent, DisableRightClickDirective
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent],

})
export class AppModule { }

Now run the application using npm start and check the result. 

In this article, we learned how to create a custom directive in Angular to disable right-click.

European AngularJS 1.7.2 SSD Hosting

Recommended European AngularJS 1.7.2 Hosting with unlimited space and bandwidth

Get free AngularJS 1.7.2 installation and a smooth running site by choosing the right foundation, and say goodbye to file upload problems, permissions issues and other AngularJS 1.7.2 hosting compatibility headaches! It just requires several click on your mouse and moreover, it is provided FREE of charge! 

HostForLIFE.eu is Microsoft No #1 Recommended Windows and ASP.NET Hosting in European Continent. Our service is ranked the highest top #1 spot in several European countries, such as: Germany, Italy, Netherlands, France, Belgium, United Kingdom, Sweden, Finland, Switzerland and many top European countries.

0 comments:

Post a Comment