Introduction
In this article, we will learn how to detect an event when clicks outside in a compoent in Angular Application.
Prerequisites
- Basic Knowledge of Angular 2 or higher
- Visual Studio Code
- Node and NPM installed
- Bootstrap
Create an Angular Project
Create an Angular project by using the following command.
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';
Now create a new component by using the following command,
ng g c actionmenu
Now open actionmenu.component.html file and add the following code.
<div class="sidebar {{show}} panel-group" clickOutside (clickOutside)="works()">
<div class=" panel panel-default" >
<div>
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
Now open actionmenu.component.ts file and add the following code.
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-actionmenu',
templateUrl: './actionmenu.component.html',
styleUrls: ['./actionmenu.component.css']
})
export class ActionmenuComponent {
@Input() show: boolean=true;
works() {
this.show = !this.show;
}
}
Now open actionmenu.component.css file and add the following code.
.sidebar {
display: none;
}
.true {
display: block;
}
.false {
display: none;
}
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">
How to detect clicks outside in Angular Application
</div>
</div>
<div class="container">
<button type="button" class="btn btn-success" (click)="show = !show">Primary</button>
<app-actionmenu [show]="show"></app-actionmenu>
</div>
Now open app.component.ts file and add the following code.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Angular App';
show:boolean= false;
}
Now create a new directive by using the following command, and the following code in this directive.
ng g directive clickOutside
import { Directive, ElementRef, Input, Output, HostListener, EventEmitter } from '@angular/core';
@Directive({
selector: '[clickOutside]'
})
export class ClickOutsideDirective {
constructor(private elementRef: ElementRef) {}
@Output() clickOutside: EventEmitter<any> = new EventEmitter();
@HostListener('document: click', ['$event.target']) onMouseEnter(targetElement:any) {
const clickInside = this.elementRef.nativeElement.contains(targetElement);
if (!clickInside) {
this.clickOutside.emit(null);
}
}
}
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 { ActionmenuComponent } from './actionmenu/actionmenu.component';
import { ClickOutsideDirective } from './click-outside.directive';
@NgModule({
declarations: [
AppComponent,
ActionmenuComponent,ClickOutsideDirective
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Now run the application using npm start and check the result.
Click on button and then click outside anywhere in the component.
Summary
In this article, we learned how to detect event when clicks outside anywhere in the compoent Angular application