How to detect click event outside Angular component ?
Last Updated :
05 Nov, 2020
In angular, clicks are detected by different ways. As click is an event, thus inside a component it is detected by simple event binding. A simple click via event binding for detection within the component is given as follows:
javascript
@Component({
selector: "geeks",
template: `
<h1 (click)="clicked()">{{ some_text }}</h1>
`
})
export class GeeksComponent {
constructor() {}
some_text = "Click Here";
clicked() {
this.some_text = "Event Triggered";
}
}
To go on detection for click outside the component, @HostListener decorator is used in angular. It is a decorator that declares a DOM event to listen for and provides a link with a handler method to run whenever that event occurs.
Approach: Here the approach is to use
@HostListener decorator. In angular, it is a decorator that helps in capturing any kind of event happening inside the DOM and gives the developer flexibility to perform any action based on that event. Here, on the simple click event, the handler will be referring the click event onto the component and for click on allover the DOM, it will capture using
document:click.
The syntax for using HostListener is given as follows:
Syntax:
@HostListener(events, args)
handler_name(args){
// Do something
}
There are three things to note in the syntax of HostListener:
- eventName: As the name suggests, this takes in the name of the event in the DOM, that needs to be listened.
- args: These are set of arguments to pass to the handler method when the event occurs. It takes input in list format.
- handlen_name: Here comes the method definition that will be called when event is triggered. It is called by the HostListener automatically.
Example: Bind with click within component For binding the click within the component, the eventName that will go inside the hostListener will be simply 'click'. The above code in such a case will be written as :
javascript
@Component({
selector: "geeks",
template: `
<h1>{{ some_text }}</h1>
`
})
export class GeeksComponent {
constructor() {}
some_text = "Click Here";
@HostListener("click")
clicked() {
this.some_text = "Event Triggered";
}
}
Output:
Here there is no need to pass arguments for the handler to run. the HostListener takes 'click' as the event which triggered the method clicked.
Bind with click outside component
For detection of the click outside the component, another event is to be taken look at. Here the click will not work as it detects click within the component. Here the key will be looking for click within the DOM and not just the component, and thus 'document:click' will be the right choice for that and also at the same time we need to filter out the event within the component which is done by the Boolean variable 'inside'. So in the code given as follows, there will be another component added to is which will act as outside context, but click on that will cause the click event on the current component.
javascript
@Component({
selector: "another",
template: `
<div style="border-style: solid;margin:5px;">
<h1>Outside Component</h1>
<h2>Click here for outer component trigger</h2>
</div>
<geeks></geeks>
`
})
export class AnotherComponent {
constructor() {}
}
@Component({
selector: "geeks",
template: `
<div style="border-style:solid;margin:5px;">
<h1>Inner Component</h1>
<h2>{{ some_text }}</h2>
</div>
`
})
export class GeeksComponent {
constructor() {}
some_text = "Click Here";
inside = false;
@HostListener("click")
clicked() {
this.inside = true;
}
@HostListener("document:click")
clickedOut() {
this.some_text = this.inside
? "Event Triggered"
: "Event Triggered Outside Component";
this.inside = false;
}
}
Output:

In this example, if there is a click on the text 'Outside Component' then the text shown will be 'Event Triggered Outside Component'. This shows how the click outside the component will be captured, within the GeeksComponent.
Similar Reads
How to detect click outside React component ? We can use the createRef() Â method to create a reference for any element in the class-based component. Then we can check whether click event occurred in the component or outside the component. In the functional component, we can use the useRef() hook to create a reference for any element. Creating R
2 min read
Angular PrimeNG Form Checkbox Events Component Angular PrimeNG is an open-source front-end UI library that has many native Angular UI components which help developers to build a fast and scalable web solution. In this article, we will be seeing Angular PrimeNG Form Checkbox Events Component. The Checkbox Component provided by PrimeNG is an exten
4 min read
Handle User Events in Angular Components User events are very important for creating interactive and responsive applications in Angular. These events allow users to interact with the user interface (UI) elements, and Angular provides a convenient way to handle these events within components. In Angular, user events can be handled using eve
3 min read
Passing data from Child to Parent Component in Angular In Angular, passing data from a child component to its parent component involves emitting events. Essentially, the child component emits an event containing the data that the parent component needs to receive. This is typically achieved using Angular's EventEmitter class, where the child component e
3 min read
Show or hide children components in Angular 10 Prerequisites: Angular must be installed  In this article, we will see how we can show or hide the child components in Angular.  Lets start by creating a new project. Create a new folder and initialize a new angular project. Run the project to verify it is working.ng new myProject ng serve -o This
2 min read
Angular PrimeNG Form InputSwitch Events Component Angular PrimeNG is a collection of UI components made for Angular applications. It makes it easy for developers to build beautiful and efficient web interfaces without investing a lot of time. In this article, we will be seeing the Angular PrimeNG Form InputSwitch Events Component. The InputSwitch C
3 min read
Angular PrimeNG Form RadioButton Events Component Angular PrimeNG is a UI component library for Angular Applications. It offers many pre-built themes and UI components for a variety of tasks like inputs, menus, charts, Buttons, etc. In this article, we will see Angular PrimeNG Form RadioButton Events Component. The PrimeNG Form Component is built o
4 min read
Angular PrimeNG Form MultiSelect Events Component Angular PrimeNG is a free and open-source framework with various components that Angular developers can use in their applications to enhance the user experience and speed up the development as they do not have to write everything from the ground up. In this article, we will be discussing the Angular
5 min read
How to get original element from ng-click in AngularJS ? In AngularJS, we can get the original DOM element that fired the ng-click event using the event's currentTarget property. Getting the event's currentTarget can be useful when we want to track an element's activity, in this case, whether and how many times a particular element is clicked, or we want
3 min read
How to call a function on click event in Angular2 ? A Function is a set of statements that takes input, does some specific computation, and produces output. An on click event occurs when the user clicks on an element. In this article, we will see How to call a function on click event in Angular2, along with understanding the basic implementation thro
3 min read