# Event Binding

Parentheses bind an event to a method of the component, such as (click)="addToCart()". $event passes the event itself, a template reference variable such as #box passes an element, and (keyup.enter) listens for one key.

Apprentice · Angular · Updated November 3, 2026

## After this lesson you can

- Run a component's method when an event happens with parentheses, such as (click)
- Pass the event or an element to the method with $event or a template reference variable

## Key takeaways

- Brackets send data into an element; parentheses bring events out: `(click)="addToCart()"` runs the method on every click.
- The name in parentheses is any DOM event without its `on`, such as `(input)` or `(keyup)`, and `(keyup.enter)` fires only for the Enter key.
- `$event` passes the browser's event object to your method.
- A template reference variable such as `#box` names an element, so a binding can pass it, as in `addItem(box.value)`.

Square brackets send data **into** an element. Parentheses bring events **out** of it: **event binding** runs a method of the component when something happens. It's `addEventListener` without the code.

## (click)

```typescript
import { Component, signal } from '@angular/core';

@Component({
  imports: [],
  selector: 'app-cart-button',
  template: `
    <button class="btn btn-primary" (click)="addToCart()">Add to cart</button>
    <p>{{ count() }} in the cart</p>
  `,
})
export class CartButton {
  protected readonly count = signal(0);

  protected addToCart() {
    this.count.update(c => c + 1);
  }
}
```

Each click runs `addToCart()`, and the page shows *1 in the cart*, *2 in the cart*, and so on.

`count` is a **signal**: a value that changes, which Angular watches. Read it by calling it, `count()`, and change it with `update`. *State with Signals* covers them properly; for now, use one whenever a component's data changes.

The name in parentheses is any DOM event, without the *on*: `(click)`, `(dblclick)`, `(mouseover)`, `(submit)`, `(input)`, `(change)`, `(keyup)`.

## $event: the event itself

Inside the quotes, **`$event`** is the browser's event object, the one `addEventListener` handed to its callback:

```html
<input class="form-control" (input)="onType($event)">
```

```typescript
protected onType(event: Event) {
  const input = event.target as HTMLInputElement;
  this.searchText.set(input.value);
}
```

`event.target` is the element; `as HTMLInputElement` tells TypeScript it's an input, so it allows `.value`.

## A template reference variable

Often simpler: give the element a name with **`#`**, a **template reference variable**, and pass the element itself:

```html
<input class="form-control" #box (keyup.enter)="addItem(box.value); box.value = ''">
<button class="btn btn-secondary" (click)="addItem(box.value); box.value = ''">Add</button>
```

```typescript
protected addItem(name: string) {
  if (name.trim() !== '') {
    this.items.update(items => [...items, name.trim()]);
  }
}
```

- **`#box`** names the input, anywhere in this template.
- **`(keyup.enter)`** fires only for the **Enter** key: Angular's shortcut for checking which key was pressed.
- One binding can run two statements, separated by `;`: add the item, then empty the box.
- `[...items, name]` makes a **new** array: the old items, plus one. Signals notice a new value, not a change inside the old one.

## Check yourself

2 questions to try on the lesson page.

## So what

Brackets in, parentheses out: `[property]` sends data to an element, `(event)` brings the user's actions back to your methods. With both, a template and its class talk in both directions.

## Key terms

- [Event Binding](https://www.ask-codey.com/glossary/event-binding)
- [Template Reference Variable](https://www.ask-codey.com/glossary/template-reference-variable)

## Cheat sheets

- [Event Binding and Two-Way Binding](https://www.ask-codey.com/cheat-sheets/binding-and-events-in-angular-event-binding-cheat-sheet)

Source: https://www.ask-codey.com/lessons/binding-and-events-in-angular-event-binding
