Event Binding in Angular 12

Event Binding in Angular 12

Angular12 16-07-2021 Saheb Sutradhar

In this post we are going to learn what is event binding in angular, event binding is most important thing without event binding we can not build an angular application.

What is event binding in angular ?

Event binding is a technique that allows us to bind JavaScript events to a HTML element . In event binding the data flow happens from view to component (one-way).

Syntax

<button (click)="function()">Click</button>

Example 1: (click event)

Create button and bind a click event to it , when the user clicks the button we will show  alert popup with this message "You have clicked the button".

 

src/app/app.component.html
<button (click)="showMessage()">Click</button>
src/app/app.component.ts
import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
 public showMessage(){
    alert("You have clicked the button");
  }
}
Output

Event Binding in Angular 12

 

Example 2: (change event)

Create checkbox and bind a change event to it , when the user check the checkbox we will show  alert popup with this message "You have checked the checkbox".

src/app/app.component.html
<label>Check Box</label>
<input type="checkbox" (change)="showMessage()">
src/app/app.component.ts
import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
 public showMessage(){
    alert("You have checked the checkbox");
  }
}
Output

Event Binding in Angular 12

 

I hope this article is helpful for you , i am very glad to help you thanks for reading

 

 

Related Posts

Card image cap

Angular Material Datepicker Example

Angular12 14-07-2021 Saheb Sutradhar

In this angular material datepicker post i will show you how to use Datepicker in angular app using angular material. ...

Card image cap

Angular Material Sidenav Example

Angular12 14-07-2021 Saheb Sutradhar

In this post you will learn how to implement angular material sidenav , in most of the realtime projects we use sidenav for the application navigation , even in ...

Card image cap

Property binding in angular 12

Angular12 14-07-2021 Saheb Sutradhar

In this post we are going to learn property binding  in angular . Before learning property binding in angular we should know the difference between HTML Attrib ...

Card image cap

Class binding in angular 12

Angular12 15-07-2021 Saheb Sutradhar

In this post we are going to see how we can bind class in angular . Class binding  in angular is very important when you work in a real time project. I have ex ...

Card image cap

Pipes in Angular 12

Angular12 16-07-2021 Saheb Sutradhar

In angular we use pipes to formate the data, thre are few built in pipes in angular. ...

Card image cap

Custom Pipe In Angular

Angular12 17-07-2021 Saheb Sutradhar

In this post you will learn how to create custom pipe in angular. When you need some data formatting which can not be done by using angular built-in pipes then ...