Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Read (event)="statement" as "when event, do statement". Then check two things: does the statement need data from the event (pass $event or a #ref), and is it longer than one method call (move it into the class).

Wiring "press Enter to add"Step 1 of 4
  1. When what?

    When Enter is pressed in the name box: (keydown.enter).

Drills

Answer fast — trust the rule.

quick drill

For (click)="water($event)" on a <button>, what is $event?

quick drill

Which binding runs save() only when Enter is pressed?

quick drill

<input #box /> sits next to a button. How can the button's click pass the typed text?

quick drill

Why might (click)="alert('Watered!')" fail?

Remember

  • (event)="statement" means "when event, do statement".
  • $event is the DOM event for native events and the emitted value for component outputs; #ref names an element.
  • Trap: logic piled into the template; keep one method call and move the rest to the class.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainAdd a plant with Enter

Greenhouse has a text box for a new plant's name. Write the event bindings so that typing updates a draft signal and pressing Enter calls addPlant(). Then explain what $event is in each binding and why the logic belongs in a method rather than in the template.