Skip to the workspace
Pebellslearn by building
Understand Binding  ›  Lesson

Event Binding and Template Statements

Event Binding 12 minRound brackets listen for a DOM or component event and run a template statement, usually a method call, with $event holding the event's payload.

angular / Event Binding and Template Statements

Explore each scene at your own pace.

01 · The rule

The rule

Event Binding and Template Statements

A plant card that cannot be watered is just a picture. Buttons, text boxes and key presses need to reach the class. Round brackets carry those events from the template into your code.

The rule

Round brackets listen; the statement inside should just call a method.

<button (click)="water()">Water</button>

When the button fires click, Angular runs the template statement water(). Unlike an expression, a statement is allowed to do things, but keep it to a method call. Real logic belongs in the class, where it can be read and tested.

To use the event itself, pass $event:

<input (input)="onType($event)" />
draft = signal('');

onType(e: Event) {
  const box = e.target as HTMLInputElement;
  this.draft.set(box.value);
}

What $event holds depends on the source:

  • A native DOM event (click, input, submit) gives the browser's event object.
  • A component output, like (watered) from the last section, gives whatever the child emitted.

Angular also offers key filters, so you do not have to check e.key yourself:

<input (keydown.enter)="addPlant()" (keydown.escape)="clear()" />

A template reference variable often removes the need for $event entirely. #box names the element, and statements can use it:

<input #box />
<button (click)="addPlant(box.value)">Add</button>

Statements see the component's members, $event and template variables, but not globals such as window or alert.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.