Skip to main content

Event Binding

Event binding in Angular lets your components respond to user actions and browser events, using the (event) syntax.

Exampleโ€‹

Here is a component that collects recipes by drag-and-dropping them:

@Component({
selector: 'mc-recipe-collector',
template: `
<h1>Drag and drop your recipes below</h1>
<div
aria-label="Recipe drop zone"
role="region"
(drop)="logRecipe($event)"
>
<p>Drop your recipes here</p>
</div>
`,
})
class RecipeCollector {
logRecipe(event: Event) {
console.log(event.dataTransfer?.files);
}
}

Here, (drop)="logRecipe($event)" is an event binding. When the user drops a recipe file, Angular calls the logRecipe() method on your component. You can bind to any standard DOM event (like click, input, keyup).

Pragmatic Angular Testing
๐Ÿ‘จ๐Ÿปโ€๐Ÿณ Let's cook some tests โ†’

๐Ÿ’ฐ 80โ‚ฌ ยท 170โ‚ฌ ยท Lifetime access

Learn how to write reliable tests that survive upgrades and refactorings.