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
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. $eventpasses the browser's event object to your method.- A template reference variable such as
#boxnames an element, so a binding can pass it, as inaddItem(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)
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:
<input class="form-control" (input)="onType($event)">
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:
<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>
protected addItem(name: string) {
if (name.trim() !== '') {
this.items.update(items => [...items, name.trim()]);
}
}
#boxnames 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.