---
title: "Explain the Events in AngularJS"  
description: "You can build dynamic, responsive, and interactive web applications by using event handling in AngularJS."  
author: "Ashutosh Patel"  
published: 2024-06-24  
updated: 2024-06-24  
canonical: https://www.mindstick.com/articles/336210/explain-the-events-in-angularjs  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs events"]  
reading_time: 3 minutes  

---

# Explain the Events in AngularJS

#### AngularJS Event Handling

In AngularJS, [event handling](https://www.mindstick.com/articles/12724/event-handling-in-android) basically involves using methods and functions to respond to [user interactions](https://www.mindstick.com/forum/158690/how-can-you-handle-user-interactions-such-as-button-clicks-using-jquery) and other events in [your application](https://answers.mindstick.com/qa/97584/how-do-you-choose-the-correct-camera-for-your-application).

Here is a detailed overview of event handling in AngularJS,

#### DOM events using directives

Directives in AngularJS allow you to extend HTML with custom behavior to include DOM manipulation and event handling logic. You can use directives to listen for DOM events such as clicks, `mouseovers`, and `keydowns`.

## 'click' event handling

```html
<div ng-app="myApp" ng-controller="MyController">
 <button ng-click="handleClick()">Click me</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('MyController', function($scope) {
 $scope.handleClick = function() {
   alert('Button clicked!');
 };
});
</script>
```

## In the example above

- `ng-click="handleClick()"` is an AngularJS directive that binds the click event to the **handleClick()** function of the controller.
- When the [button is clicked](https://answers.mindstick.com/qa/94813/why-does-my-app-terminate-when-the-button-is-clicked-on-android-studio), the handleClick() function is executed, displaying a warning message.

#### Using the `$event` Object

You can pass the $event object to its event handler to obtain information about the event, such as target element, key codes, or mouse coordinates.

## $event passed to the Event Handler

```html
<div ng-app="myApp" ng-controller="MyController">
 <button ng-click="handleClick($event)">Click me</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('MyController', function($scope) {
 $scope.handleClick = function(event) {
   alert('Button clicked! Target element: ' + event.target.tagName);
 };
});
</script>
```

#### Custom Event Handling

AngularJS also supports custom event handling using $emit, $broadcast, and $on methods to communicate between controllers and directives.

## Custom Event Handling

```html
<div ng-app="myApp">
 <div ng-controller="ParentController">
   <button ng-click="broadcastEvent()">Broadcast Event</button>
 </div>
 <div ng-controller="ChildController">
   <p>{{ message }}</p>
 </div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('ParentController', function($scope) {
 $scope.broadcastEvent = function() {
   $scope.$broadcast('customEvent', 'Data from parent');
 };
});
app.controller('ChildController', function($scope) {
 $scope.$on('customEvent', function(event, data) {
   $scope.message = 'Event received in child: ' + data;
 });
});
</script>
```

#### \
Event Delegation

AngularJS supports [event delegation](https://www.mindstick.com/forum/157814/how-does-event-delegation-work-in-jquery) which allows you to handle events on parent elements instead of binding [event handlers](https://answers.mindstick.com/qa/93763/how-to-add-event-handlers-on-html-elements-by-javascript) directly to child elements. This can [improve performance](https://www.mindstick.com/interview/34405/how-do-you-improve-performance-of-large-python-applications), especially with large lists or sharply designed objects.\

## Event Delegation

```html
<div ng-app="myApp" ng-controller="MyController" ng-click="handleClick($event)">
 <button ng-click="handleClick($event)">Click me</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('MyController', function($scope) {
 $scope.handleClick = function(event) {
   if (event.target.tagName === 'BUTTON') {
     alert('Button clicked!');
   }
 };
});
</script>
```

#### Overview

**Directives-** Handle DOM events declaratively with ng-click, ng-change, and ng-keydown.\
**Controllers-** [Define functions](https://www.mindstick.com/interview/33884/how-do-you-define-functions-in-css-and-benefits) to handle events triggered by instructions.\
**Custom Events-** Use $emit, $broadcast, and $on to handle scheduled events between controllers and instructions.\
**Event Delegation-** Modify events on parent elements to improve performance and manage [dynamic content](https://www.mindstick.com/forum/158483/how-does-server-side-caching-interact-with-dynamic-content-or-user-specific-data-on-a-website).

**Also, Read:** [Explain the custom directives in AngularJS](https://www.mindstick.com/articles/336208/explain-the-custom-directives-in-angularjs)

---

Original Source: https://www.mindstick.com/articles/336210/explain-the-events-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
