Author -  Sai gowtham

Angular Event handling tutorial

In this tutorial, we are going to learn about how to handle the events in angular with the help of examples.

Listening events

To listen the events in angular we need to wrap the eventname with a () parenthesis.

app.component.html
<!-- listening for the click event -->
<button (click)="handleClick()">Click me</button>

In the above code we are listening for the click event on a button, so that whenever a user clicks on a button the handleClick method is invoked.

Passing arguments

Event handler methods can also accept arguments let’s see an example.

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'angular tutorial';   // at time of invocation we need to pass an argument
  changeName(myname: string): void {    this.name = myname;  }}
app.component.html
<div>
    <h1>{{name}}</h1>     <!-- passing the argument to  `changeName` method -->
    <button (click)="changeName('gowtham')">Change name</button></div>

In the above code ,first we created a changeName method with parameter myname inside our app.component.ts file, so that inside our markup we are passing the gowtham as an argument to changeName method.

Accessing the event object

To access the event object in handler methods, we need to use the $event variable let’s see an example.

app.component.html
  <!-- passing the `$event` as an argument -->
<button (click)="handleMe($event)">click me</button>
app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
   handleMe(event:any){       //logging event object     console.log(event)   }

}

accesing-event-object-angular

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY