Introduction

This article will explain Event Data Binding in Angular. I will discuss event binding with examples.

How to handle events in Angular?

Any activity like button click, mouse click, mouse hover, mouse move, etc. of a user on the front-end or a web screen is termed as an event. Such events are passed from the View (.HTML) page to a TypeScript component (.ts).

List of the events in Angular applications:

  • Click: The event occurs when the user clicks on an element
  • Dbclick: The event occurs when the user double-clicks on an element
  • Blur: The event occurs when an element loses focus
  • Submit: The event occurs when a form is submitted
  • Focus: The event occurs when an element gets focus
  • Scroll: The event occurs when an element's scrollbar is being scrolled
  • Cut: The event occurs when the user cuts the content of an element
  • Copy: The event occurs when the user copies the content of an element
  • Paste: The event occurs when the user pastes some content in an element
  • Keyup: The event occurs when the user releases a key
  • Keypress: The event occurs when the user presses a key
  • Keydown: The event occurs when the user is pressing a key
  • Mouseup: The event occurs when a user releases a mouse button over an element
  • Mousedown: The event occurs when the user presses a mouse button over an element
  • Mouseenter: The event occurs when the pointer is moved onto an element
  • Drag: The event occurs when an element is being dragged
  • Drop: The event occurs when the dragged element is dropped on the drop target
  • Dragover: The event occurs when the dragged element is over the drop target
Let us see this with a demo.
Step 1
Open a command prompt from Windows Search.
Step 2
Create a new project in Angular.
ng new AngularDemo
Step 3
Open the project in Visual Studio Code and type the following command to open the project:
Code .
Step 4
Open terminal in Visual Studio Code and create a component "employee".
ng g c example
Step 5
Open the example component in your application.
  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-example',
  4. templateUrl: './example.component.html',
  5. styleUrls: ['./example.component.css']
  6. })
  7. export class ExampleComponent {
  8. message='';
  9. onClickMe()
  10. {
  11. this.message="Welcome to angular programming..."
  12. }
  13. }
Step 6
Open example.component.html in your application.
  1. button class="btn btn-primary" (click)="onClickMe()">Click me!</button>
  2. <h2>{{message}}</h2>
Step 7
Open app.component.html and take the selector name from employee.component.ts.
  1. < <app-example></app-example>
Step 8
Run the application by typing the following command.
ng serve –open
Event Data Binding In Angular
DoubleClick Event in angular
  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-example',
  4. templateUrl: './example.component.html',
  5. styleUrls: ['./example.component.css']
  6. })
  7. export class ExampleComponent {
  8. message = ''
  9. myFunction() {
  10. this.message = "You have double clicked";
  11. }
  12. }
  13. <h4 class="dbClick" (dblclick)="myFunction()">Double-click me</h4>
  14. <h3 class="text-success">{{message}}</h3>
Event Data Binding In Angular
Add employee Keyup event
  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-example',
  4. templateUrl: './example.component.html',
  5. styleUrls: ['./example.component.css']
  6. })
  7. export class ExampleComponent {
  8. employees=['Farhan Ahmed'];
  9. addEmployee(newEmployee:string){
  10. if(newEmployee){
  11. this.employees.push(newEmployee);
  12. }
  13. }
  14. <div>
  15. <div class="col-md-4">
  16. <div class="form-group">
  17. <label>Enter Employee Name:</label>
  18. <div class="input-group mb-3">
  19. <input class="form-control" #newEmployee (keyup.enter)="addEmployee(newEmployee.value)"
  20. (blur)="newEmployee(newEmployee.value); newEmployee.value=''">
  21. <div class="input-group-append">
  22. <button class="btn btn-primary" (click)="addEmployee(newEmployee.value)">Add New</button>
  23. </div>
  24. </div>
  25. </div>
  26. <li *ngFor="let emp of employees">{{emp}}</li>
  27. </div>
  28. </div>
Event Data Binding In Angular

Summary

In this article, I have explained Event Data Binding in Angular applications. I hope you understood the concept.