So far data flows only one way: from the class to the page. The page can show a price, but the "+1" button next to it can't do anything yet when it's clicked.
Event binding reverses the direction. The template listens for an event (something that happens on the page), such as a click or typing. Then it runs code in the class, usually a method (a function that belongs to the class). It's written with round brackets: (click)="increment()". Picture the bell on the counter at Jali's Pancake Stall. A customer presses the bell (the event), Jali hears it and writes the order down (the method), and the order board on the wall updates (the template). Square brackets [ ] carry data from the class to the page. Round brackets ( ) carry events from the page to the class.
(click): from a click to a method
This order component has two buttons, -1 and +1:
import { Component } from '@angular/core'
@Component({
selector: 'app-order',
standalone: true,
template: `
<h3>Special Egg Pancake · $10</h3>
<button (click)="decrement()">-1</button>
<button (click)="increment()">+1</button>
<p>Count: {{ count }}</p>
<p>Total: {{ '$' + count * 10 }}</p>
`,
})
export class Order {
count = 0
increment() {
this.count = this.count + 1
}
decrement() {
if (this.count > 0) {
this.count = this.count - 1
}
}
}
What shows after each click:
| Event | count |
Shows |
|---|---|---|
| First shown | 0 | Count: 0, Total: $0 |
| Click +1 | 1 | Count: 1, Total: $10 |
| Click +1 | 2 | Count: 2, Total: $20 |
| Click -1 | 1 | Count: 1, Total: $10 |
(click)="increment()": the event's name goes inside the brackets, and what's inside the quotes is a template statement, the code that runs when that event happens.- Inside a method you write
this.count. In the templatecountis enough, because a template always reads from its own class. - After a handler (the code that handles the event, here
increment()) finishes, Angular checks the template again and updates whatever text changed. That's why the numbers afterCount:andTotal:go up without you touching the DOM. - In modern Angular, a number like this is more and more often kept in a signal (a value holder that tells Angular when its value changes). For example
count = signal(0), changed withthis.count.update(n => n + 1), and read in the template with{{ count() }}. Signals come in the Signals lesson. The exercise here accepts a plain property.
$event: what happened
This note box uses two events, and its methods are in the second block:
<input (input)="updateNote($event)" (keyup.enter)="send()" placeholder="Note" />
<p>Note: {{ note }}</p>
note = ''
sent = ''
updateNote(event: Event) {
const input = event.target as HTMLInputElement
this.note = input.value
}
send() {
this.sent = this.note
}
$eventis the DOM event object, which holds details about what happened. The methodupdateNotereceives it asevent. For(input),event.targetis the input box, and.valueis what's in it. Every typed letter fires(input)once.as HTMLInputElementtells TypeScript the target is an input box, so.valuemay be read.(keyup.enter)="send()"runs only when the Enter key is released. You don't have to check which key was released: Angular checks it for you.
Template statements: what you may write
Allowed:
- calling a method:
(click)="increment()" - assignment (putting a value into something) with
=:(click)="count = count + 1"or(click)="favorite = !favorite" - several steps, separated by semicolons:
(click)="increment(); save()"
Before you read on, take a guess: the button <button (click)="count++">+1</button> is clicked three times. What is count now?
Refused by the compiler, with the messages from Angular 22 (the exercise editor's version):
| Written | Message |
|---|---|
(click)="count++" |
Parser Error: Unexpected end of expression: count++ at the end of the expression [count++] |
(click)="new Date()" |
Parser Error: Unexpected token 'Date' |
So count is still 0. A compile error makes the whole template fail: the button is never created, so there's nothing to click, however many times you try. this.count++ inside a method is still fine, because that's plain TypeScript, not a template.
(click)="count += 1" is refused by Angular 18 (Parser Error: Unexpected token = at column 9 in [count += 1]). Angular 22 accepts it: in the exercise editor, that button raises count. In a project still on Angular 18, write count = count + 1.
( ) versus [ ]
[disabled]="soldOut": data flows from the class to the page.(click)="increment()": events flow from the page to the class.
If the brackets are swapped, <input [keyup.enter]="send()"> runs nothing on Enter. Angular treats it as a property, and the console writes NG0303: Can't bind to 'keyup.enter' since it isn't a known property of 'input'. The right form is (keyup.enter)="send()".
⚠️ You click, and nothing changes
++in a template. The template fails to compile. Writecount = count + 1, or call a method that raises the number.+=fails too in Angular 18.- Forgetting the call brackets.
(click)="increment"doesn't error, but doesn't run anything either: the method is only named, not called. Writeincrement(). - Swapped brackets.
[click]or[keyup.enter]is treated as a property. Events always use( ). - Order in the exercise. The grader (the program that checks your answer) reads the first
<p>and presses the first<button>on the page. Put the+1button and theCount: ...paragraph with no other button or paragraph above them, and make sureCount: 0shows before any click.
TL;DR: Event binding runs code in the class when an event (something that happens on the page, such as a click) happens. It's written (event)="statement": (click)="increment()" calls a method (a function that belongs to the class), and afterwards Angular updates the template by itself. $event carries the event object (details about what happened), such as event.target.value from (input), which is the typed text. (keyup.enter) runs only when the Enter key is released. A template statement (the code inside the quotes) may call methods, use = and separate steps with semicolons. But the compiler refuses ++ and new (and += in Angular 18), and then the whole component doesn't show. ( ) carries events from the page to the class, the opposite of [ ], which carries data from the class to the page.
⚡ (click) calls a method
Write:
<button (click)="increment()">+1</button>
<p>Count: {{ count }}</p>
Meaning: This is event binding. (click) listens for a click event (something that happens on the page) on the button, then runs what's in the quotes. Here that calls increment(), a method (a function that belongs to the class) that raises the property count by one. After increment() finishes, Angular updates the template by itself, so the number in Count: goes up without you touching the DOM.
In the exercise, the grader (the program that checks your answer) reads the first <p> and presses the first <button>, so don't put another button or paragraph above them.
⚡ ++ is refused in a template
Guess first: count starts at 0, then <button (click)="count++">+1</button> is clicked three times. What is count now?
Still 0. The template fails to compile (to be turned into JavaScript), so the button is never created and there's nothing to click.
Meaning: In a template, write count = count + 1 or call a method. this.count++ inside a method is still fine, because a method's code is plain TypeScript, not a template.
⚡ $event and (keyup.enter)
Write:
<input (input)="updateNote($event)" (keyup.enter)="send()" />
Meaning: (input) runs every time a letter is typed. $event is the event object (details about what happened). The method updateNote receives it as event, and event.target.value is what's in the input box. (keyup.enter) runs only when Enter is released. If the brackets are swapped, [keyup.enter] runs nothing and the console writes error NG0303.