2020-09-18 20:43:40 +00:00
# Event binding
2020-04-28 20:26:58 +00:00
2021-07-21 05:01:39 +00:00
Event binding lets you listen for and respond to user actions such as keystrokes, mouse movements, clicks, and touches.
2020-04-28 20:26:58 +00:00
< div class = "alert is-helpful" >
See the < live-example > < / live-example > for a working example containing the code snippets in this guide.
< / div >
2022-04-14 21:48:51 +00:00
## Prerequisites
* [Basics of components ](guide/architecture-components )
* [Basics of templates ](guide/glossary#template )
* [Binding syntax ](guide/binding-syntax )
* [Template statements ](guide/template-statements )
2020-09-18 20:43:40 +00:00
## Binding to events
2020-04-28 20:26:58 +00:00
2023-09-01 20:39:54 +00:00
< div class = "alert is-helpful" >
For information on binding to properties, see [Event binding ](guide/property-binding ).
< / div >
2020-09-18 20:43:40 +00:00
To bind to an event you use the Angular event binding syntax.
This syntax consists of a target event name within parentheses to the left of an equal sign, and a quoted template statement to the right.
2020-04-28 20:26:58 +00:00
2022-04-14 21:48:51 +00:00
Create the following example; the target event name is `click` and the template statement is `onSave()` .
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2022-04-14 21:48:51 +00:00
< code-example language = "html" header = "Event binding syntax" >
2020-11-10 12:04:10 +00:00
< button (click)="onSave()"> Save< /button>
2020-09-18 20:43:40 +00:00
< / code-example >
2020-04-28 20:26:58 +00:00
2020-09-18 20:43:40 +00:00
The event binding listens for the button's click events and calls the component's `onSave()` method whenever a click occurs.
2020-04-28 20:26:58 +00:00
2020-09-18 20:43:40 +00:00
< div class = "lightbox" >
2022-04-14 21:48:51 +00:00
< img src = 'generated/images/guide/template-syntax/syntax-diagram.svg' alt = "Syntax diagram" >
2020-09-18 20:43:40 +00:00
< / div >
2020-04-28 20:26:58 +00:00
2022-04-14 21:48:51 +00:00
### Determining an event target
2021-05-24 22:33:44 +00:00
2022-04-14 21:48:51 +00:00
To determine an event target, Angular checks if the name of the target event matches an event property of a known directive.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2022-04-14 21:48:51 +00:00
Create the following example: (Angular checks to see if `myClick` is an event on the custom `ClickDirective` )
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2022-04-14 21:48:51 +00:00
< code-example path = "event-binding/src/app/app.component.html" region = "custom-directive" header = "src/app/app.component.html" > < / code-example >
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2022-04-14 21:48:51 +00:00
If the target event name, `myClick` fails to match an output property of `ClickDirective` , Angular will instead bind to the `myClick` event on the underlying DOM element.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2022-08-02 23:22:09 +00:00
## Binding to keyboard events
You can bind to keyboard events using Angular's binding syntax. You can specify the key or code that you would like to bind to keyboard events. They `key` and `code` fields are a native part of the browser keyboard event object. By default, event binding assumes you want to use the `key` field on the keyboard event. You can also use the `code` field.
Combinations of keys can be separated by a `.` (period). For example, `keydown.enter` will allow you to bind events to the `enter` key. You can also use modifier keys, such as `shift` , `alt` , `control` , and the `command` keys from Mac. The following example shows how to bind a keyboard event to `keydown.shift.t` .
2023-11-09 02:18:07 +00:00
```html
2022-08-02 23:22:09 +00:00
< input ( keydown . shift . t ) = " onKeydown ( $ event ) " / >
```
2022-08-30 14:34:53 +00:00
Depending on the operating system, some key combinations might create special characters instead of the key combination that you expect. MacOS, for example, creates special characters when you use the option and shift keys together. If you bind to `keydown.shift.alt.t` , on macOS, that combination produces a `ˇ` character instead of a `t` , which doesn't match the binding and won't trigger your event handler. To bind to `keydown.shift.alt.t` on macOS, use the `code` keyboard event field to get the correct behavior, such as `keydown.code.shiftleft.altleft.keyt` shown in this example.
2022-08-02 23:22:09 +00:00
2023-11-09 02:18:07 +00:00
```html
2022-08-02 23:22:09 +00:00
< input ( keydown . code . shiftleft . altleft . keyt ) = " onKeydown ( $ event ) " / >
```
2022-08-30 14:34:53 +00:00
The `code` field is more specific than the `key` field. The `key` field always reports `shift` , whereas the `code` field will specify `leftshift` or `rightshift` . When using the `code` field, you might need to add separate bindings to catch all the behaviors you want. Using the `code` field avoids the need to handle OS specific behaviors such as the `shift + option` behavior on macOS.
2022-08-02 23:22:09 +00:00
For more information, visit the full reference for [key ](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values ) and [code ](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values ) to help build out your event strings.
2023-09-01 20:39:54 +00:00
## Binding to passive events
Angular also supports [passive event ](https://developer.chrome.com/en/docs/lighthouse/best-practices/uses-passive-event-listeners/ ) listeners.
This is an advanced technique that is not necessary for most applications. You may find this useful if you need to optimize handling of frequently occurring events that are causing performance problems.
For example, use the following steps to make a scroll event passive.
1. Create a file `zone-flags.ts` under `src` directory.
2. Add the following line into this file.
```typescript
(window as any)['__zone_symbol__PASSIVE_EVENTS'] = ['scroll'];
```
3. In the `src/polyfills.ts` file, before importing zone.js, import the newly created `zone-flags` .
```typescript
import './zone-flags';
import 'zone.js'; // Included with Angular CLI.
```
After those steps, if you add event listeners for the `scroll` event, the listeners will be `passive` .
2020-09-18 20:43:40 +00:00
## What's next
2020-04-28 20:26:58 +00:00
2022-04-14 21:48:51 +00:00
* For more information on how event binding works, see [How event binding works ](guide/event-binding-concepts ).
* [Property binding ](guide/property-binding )
* [Text interpolation ](guide/interpolation )
* [Two-way binding ](guide/two-way-binding )
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2023-09-01 20:39:54 +00:00
@reviewed 2023-09-01