WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Angular · Basic · question 15 of 100

How do you handle events in Angular, and can you provide an example of event binding?

📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

Handling events in Angular involves using event binding. Event binding is a one-way data binding technique that listens to a DOM (Document Object Model) event (like click, submit, keyup, etc.) and executes an associated TypeScript function or method when that event is triggered.

To use event binding in Angular, you can follow these steps:

1. First, create a TypeScript method or function in your component file that you want to execute when the event is triggered. 2. Next, use the Angular event binding syntax in your HTML template to bind the DOM event to the TypeScript method.

Now, let’s create an example to better understand how to use event binding in Angular.

Let’s assume we are building a simple calculator application, and we want to display an alert message with the sum of two numbers when the "Calculate" button is clicked.

1. First, create a TypeScript method in your component file (app.component.ts) that will perform the calculation:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-event-binding-example';

  calculateSum(a: number, b: number) {
    const sum = a + b;
    alert(`The sum is: ${sum}`);
  }
}

2. Next, use the Angular event binding syntax in your HTML template (app.component.html) to bind the click event to the ‘calculateSum‘ method:

<div>
  <input type="number" [(ngModel)]="num1" placeholder="Enter first number">
  <input type="number" [(ngModel)]="num2" placeholder="Enter second number">
  
  <button (click)="calculateSum(num1, num2)">
    Calculate
  </button>
</div>

Explanation:

- We use the ‘(click)‘ syntax to bind the click event of the button to the ‘calculateSum‘ method.

- The ‘calculateSum‘ method takes two arguments, ‘num1‘ and ‘num2‘, which are connected to the respective ‘<input>‘ elements via two-way data binding (‘[(ngModel)]‘).

When you click the "Calculate" button, the ‘calculateSum(num1, num2)‘ method will be executed and display an alert with the sum of ‘num1‘ and ‘num2‘. This is a simple example of how to use event binding in Angular to handle events and associate them with custom functions or methods in your components.

Let me know if you need any more examples or a deeper explanation!

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Angular interview — then scores it.
📞 Practice Angular — free 15 min
📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

All 100 Angular questions · All topics