Angular Event Binding Tutorial 🎯

beginner
24 min

Angular Event Binding Tutorial 🎯

Welcome to our comprehensive guide on Angular Event Binding! This tutorial is designed for beginners and intermediates, covering the concept from the ground up. Let's dive into the world of Angular and understand how Event Binding works.

Understanding Event Binding 📝

Event Binding in Angular is a mechanism that allows us to handle events in HTML and pass data from the view to the component. It's a powerful tool that makes our applications more interactive and user-friendly.

Why Event Binding? 💡

  • Enables interaction between HTML and Angular components
  • Makes our application more dynamic and responsive
  • Simplifies code by separating the template and the component logic

Basic Event Binding 💡

Let's start with the simplest form of Event Binding - the (eventName) syntax.

html
<button (click)="onClick()">Click me!</button>

In the above example, we have a button. The (click) part is Angular's syntax for Event Binding. When the button is clicked, the onClick() function in our component will be called.

Creating a Component 📝

Before we move on, let's create a simple component to practice Event Binding.

bash
ng generate component event-binding

Now, let's navigate to the newly created component and add the above button in the template. Don't forget to import the Component into the App Module.

typescript
import { Component } from '@angular/core'; @Component({ selector: 'app-event-binding', template: ` <button (click)="onClick()">Click me!</button> ` }) export class EventBindingComponent { onClick() { console.log('Button clicked!'); } }

Now, if you run the application, you should see a button that logs "Button clicked!" in the console when clicked.

Two-Way Data Binding 💡

Angular also allows us to use Event Binding for Two-Way Data Binding using the [(ngModel)] syntax.

html
<input [(ngModel)]="userName" placeholder="Your Name">

In the above example, we have an input field. The [(ngModel)] part is Angular's syntax for Two-Way Data Binding. The value of the input field will be two-way bound to the userName property in our component.

Creating a Two-Way Data Binding Component 📝

Let's create a new component for Two-Way Data Binding.

bash
ng generate component two-way-binding

Now, let's navigate to the newly created component and add the above input field in the template. Don't forget to import the Component into the App Module.

typescript
import { Component } from '@angular/core'; @Component({ selector: 'app-two-way-binding', template: ` <input [(ngModel)]="userName" placeholder="Your Name"> <p>Hello, {{ userName }}</p> ` }) export class TwoWayBindingComponent { userName = ''; }

Now, if you run the application, you should see an input field and a paragraph. The paragraph will display the value entered in the input field.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the purpose of Event Binding in Angular?

Remember, this is just the beginning of our Angular Event Binding journey! In the next lessons, we will delve deeper into advanced topics and real-world examples. Happy learning! 🎉