Event Binding In Angular

Introduction

In this article, we are going to learn how the event binding works in an Angular application. As we know, in any module, an update on property value in component class will update the view template, that is just the data binding. But sometimes we need the data flow on the basis of some event. The event can be a click of a button, movement of a mouse, double-click, on a mouseover etc. So, the data binding is simply the binding of property value in component class to the HTML template, but the event binding binds the value from template to the component class.

Event Binding in angular 

Prerequisites

Let us create a sample TestApp. For this you should have installed the below for development environment:

  1. Node
  2. Npm (comes when you install node)
  3. Angular CLI.
  4. Text Editor.

For creating a new application run the below command on your location.

> ng new TestApp

Create a new component.

> ng g c test

Once your command is completed you will have TestApp folder created inside your sample folder.

Now, you will have your project folder called 'TestApp'.

Note
See my previous article “Getting started with Angular CLI.” If you want the installation and introduction from the basics then start with the execution of the sample application.

Let us start with the simple property and set the value on click event to the class property and update back to the template.

Open test.component.html and add the below content,

  1. <button (click)='setName()'>setName</button>  
  2. <p>Welcome : {{name}}</p> 

Open test.component.ts and add the below content,

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public name = '';  
  11.   setName(){  
  12.     this.name = 'Mohammad Irshad';  
  13.   }  
  14.    
  15.   constructor() { }  
  16.   ngOnInit() {  
  17.   }  
  18. }  

Event Binding in angular

Click on ‘setName’ button.
 
Event Binding in angular 

You can see the event is being called and the name is set in your class property and with the help of data binding the value is bound back to the view.

So, with the help of interpolation, we are able to set the value to the view HTML. If you want to send information about the event that is being called, use $ event, which is the special variable of Angular that will give you all the information about the event raised.

Let us pass this variable to click event. Modify the content of test.component.html

  1. <button (click)='setName($event)'>Set Name</button>  
  2. <p>Welcome : {{name}}</p>  

On your test.component.ts add the below content.

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public name = '';  
  11.   setName(event){  
  12.     console.log(event);  
  13.     this.name = 'Mohammad Irshad';  
  14.   }  
  15.   constructor() { }  
  16.   ngOnInit() {  
  17.   }  
  18. }  

Run the application.

Event Binding in angular

After clicking on ‘Set Name’ button.

Event Binding in angular

You can see in the console about the type of event, like MouseEvent and various parameters like its coordinate, type etc. So with the help of $ event, you will have access to the various properties of the DOM event.

You can also have method statement inside the HTML element. So if you have only a single line of the statement then you can directly write it there. It will be readable and done without any handler event.

  1. <button (click)=”name=’Mohammad Irshad’”>setName</button>  
  2. Welcome : {{name}}  

So, this is the basics of the event that will capture an event and perform the activities related to it.


Similar Articles