2019-12-02 15:47:19 -05:00
|
|
|
# User input
|
2017-03-31 19:57:13 -04:00
|
|
|
|
2020-10-16 15:50:28 -04:00
|
|
|
<div class="callout is-critical">
|
|
|
|
<header>Marked for archiving</header>
|
|
|
|
|
|
|
|
To ensure that you have the best experience possible, this topic is marked for archiving until we determine
|
|
|
|
that it clearly conveys the most accurate information possible.
|
|
|
|
|
|
|
|
In the meantime, this topic might be helpful: [Event binding](guide/event-binding).
|
|
|
|
|
|
|
|
If you think this content should not be archived, please file a [GitHub issue](https://github.com/angular/angular/issues/new?template=3-docs-bug.md).
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
User actions such as clicking a link, pushing a button, and entering
|
|
|
|
text raise DOM events.
|
|
|
|
This page explains how to bind those events to component event handlers using the Angular
|
|
|
|
event binding syntax.
|
|
|
|
|
|
|
|
Run the <live-example></live-example>.
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Binding to user input events
|
|
|
|
|
2020-04-28 16:26:58 -04:00
|
|
|
You can use [Angular event bindings](guide/event-binding)
|
2017-02-22 13:09:39 -05:00
|
|
|
to respond to any [DOM event](https://developer.mozilla.org/en-US/docs/Web/Events).
|
|
|
|
Many DOM events are triggered by user input. Binding to these events provides a way to
|
|
|
|
get input from the user.
|
|
|
|
|
|
|
|
To bind to a DOM event, surround the DOM event name in parentheses and assign a quoted
|
2020-04-28 16:26:58 -04:00
|
|
|
[template statement](guide/template-statements) to it.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
The following example shows an event binding that implements a click handler:
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/click-me.component.ts" region="click-me-button" header="src/app/click-me.component.ts"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
{@a click}
|
|
|
|
|
|
|
|
The `(click)` to the left of the equals sign identifies the button's click event as the **target of the binding**.
|
2017-02-22 13:09:39 -05:00
|
|
|
The text in quotes to the right of the equals sign
|
|
|
|
is the **template statement**, which responds
|
|
|
|
to the click event by calling the component's `onClickMe` method.
|
|
|
|
|
|
|
|
When writing a binding, be aware of a template statement's **execution context**.
|
|
|
|
The identifiers in a template statement belong to a specific context object,
|
|
|
|
usually the Angular component controlling the template.
|
|
|
|
The example above shows a single line of HTML, but that HTML belongs to a larger component:
|
|
|
|
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/click-me.component.ts" region="click-me-component" header="src/app/click-me.component.ts"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
When the user clicks the button, Angular calls the `onClickMe` method from `ClickMeComponent`.
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Get user input from the $event object
|
|
|
|
DOM events carry a payload of information that may be useful to the component.
|
|
|
|
This section shows how to bind to the `keyup` event of an input box to get the user's input after each keystroke.
|
|
|
|
|
|
|
|
The following code listens to the `keyup` event and passes the entire event payload (`$event`) to the component event handler.
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/keyup.components.ts" region="key-up-component-1-template" header="src/app/keyup.components.ts (template v.1)"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
When a user presses and releases a key, the `keyup` event occurs, and Angular provides a corresponding
|
|
|
|
DOM event object in the `$event` variable which this code passes as a parameter to the component's `onKey()` method.
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/keyup.components.ts" region="key-up-component-1-class-no-type" header="src/app/keyup.components.ts (class v.1)"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
The properties of an `$event` object vary depending on the type of DOM event. For example,
|
2018-03-07 13:23:30 -05:00
|
|
|
a mouse event includes different information than an input box editing event.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
All [standard DOM event objects](https://developer.mozilla.org/en-US/docs/Web/API/Event)
|
|
|
|
have a `target` property, a reference to the element that raised the event.
|
|
|
|
In this case, `target` refers to the [`<input>` element](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) and
|
|
|
|
`event.target.value` returns the current contents of that element.
|
|
|
|
|
|
|
|
After each call, the `onKey()` method appends the contents of the input box value to the list
|
2019-10-26 10:35:03 -04:00
|
|
|
in the component's `values` property, followed by a separator character (|).
|
2020-04-28 16:26:58 -04:00
|
|
|
The [interpolation](guide/interpolation)
|
2017-04-26 08:11:02 -04:00
|
|
|
displays the accumulating input box changes from the `values` property.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
Suppose the user enters the letters "abc", and then backspaces to remove them one by one.
|
|
|
|
Here's what the UI displays:
|
2017-03-30 15:04:18 -04:00
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
<code-example>
|
|
|
|
a | ab | abc | ab | a | |
|
|
|
|
</code-example>
|
|
|
|
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
2019-11-11 17:47:51 -05:00
|
|
|
<div class="lightbox">
|
|
|
|
<img src='generated/images/guide/user-input/keyup1-anim.gif' alt="key up 1">
|
|
|
|
</div>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
2018-07-19 18:00:08 -04:00
|
|
|
<div class="alert is-helpful">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
Alternatively, you could accumulate the individual keys themselves by substituting `event.key`
|
|
|
|
for `event.target.value` in which case the same user input would produce:
|
2017-03-30 15:04:18 -04:00
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
<code-example>
|
2017-03-31 19:57:13 -04:00
|
|
|
a | b | c | backspace | backspace | backspace |
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
</code-example>
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
</div>
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
{@a keyup1}
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
### Type the _$event_
|
|
|
|
|
|
|
|
The example above casts the `$event` as an `any` type.
|
2017-04-26 08:11:02 -04:00
|
|
|
That simplifies the code at a cost.
|
2017-02-22 13:09:39 -05:00
|
|
|
There is no type information
|
|
|
|
that could reveal properties of the event object and prevent silly mistakes.
|
|
|
|
|
|
|
|
The following example rewrites the method with types:
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/keyup.components.ts" region="key-up-component-1-class" header="src/app/keyup.components.ts (class v.1 - typed )"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
The `$event` is now a specific `KeyboardEvent`.
|
2017-02-22 13:09:39 -05:00
|
|
|
Not all elements have a `value` property so it casts `target` to an input element.
|
|
|
|
The `OnKey` method more clearly expresses what it expects from the template and how it interprets the event.
|
|
|
|
|
|
|
|
### Passing _$event_ is a dubious practice
|
|
|
|
Typing the event object reveals a significant objection to passing the entire DOM event into the method:
|
|
|
|
the component has too much awareness of the template details.
|
|
|
|
It can't extract information without knowing more than it should about the HTML implementation.
|
2017-04-26 08:11:02 -04:00
|
|
|
That breaks the separation of concerns between the template (_what the user sees_)
|
2017-02-22 13:09:39 -05:00
|
|
|
and the component (_how the application processes user data_).
|
|
|
|
|
|
|
|
The next section shows how to use template reference variables to address this problem.
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Get user input from a template reference variable
|
|
|
|
There's another way to get the user data: use Angular
|
2020-04-28 16:26:58 -04:00
|
|
|
[**template reference variables**](guide/template-reference-variables).
|
2017-02-22 13:09:39 -05:00
|
|
|
These variables provide direct access to an element from within the template.
|
|
|
|
To declare a template reference variable, precede an identifier with a hash (or pound) character (#).
|
|
|
|
|
|
|
|
The following example uses a template reference variable
|
|
|
|
to implement a keystroke loopback in a simple template.
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/loop-back.component.ts" region="loop-back-component" header="src/app/loop-back.component.ts"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
The template reference variable named `box`, declared on the `<input>` element,
|
|
|
|
refers to the `<input>` element itself.
|
|
|
|
The code uses the `box` variable to get the input element's `value` and display it
|
|
|
|
with interpolation between `<p>` tags.
|
|
|
|
|
|
|
|
The template is completely self contained. It doesn't bind to the component,
|
|
|
|
and the component does nothing.
|
|
|
|
|
|
|
|
Type something in the input box, and watch the display update with each keystroke.
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
2019-11-11 17:47:51 -05:00
|
|
|
<div class="lightbox">
|
|
|
|
<img src='generated/images/guide/user-input/keyup-loop-back-anim.gif' alt="loop back">
|
|
|
|
</div>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
|
2018-07-19 18:00:08 -04:00
|
|
|
<div class="alert is-helpful">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
**This won't work at all unless you bind to an event**.
|
|
|
|
|
|
|
|
Angular updates the bindings (and therefore the screen)
|
|
|
|
only if the app does something in response to asynchronous events, such as keystrokes.
|
|
|
|
This example code binds the `keyup` event
|
2017-04-26 08:11:02 -04:00
|
|
|
to the number 0, the shortest template statement possible.
|
|
|
|
While the statement does nothing useful,
|
2017-03-27 11:08:53 -04:00
|
|
|
it satisfies Angular's requirement so that Angular will update the screen.
|
|
|
|
|
2017-04-10 11:51:13 -04:00
|
|
|
</div>
|
2017-03-27 11:08:53 -04:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
It's easier to get to the input box with the template reference
|
2017-02-22 13:09:39 -05:00
|
|
|
variable than to go through the `$event` object. Here's a rewrite of the previous
|
|
|
|
`keyup` example that uses a template reference variable to get the user's input.
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/keyup.components.ts" region="key-up-component-2" header="src/app/keyup.components.ts (v2)"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
A nice aspect of this approach is that the component gets clean data values from the view.
|
|
|
|
It no longer requires knowledge of the `$event` and its structure.
|
2017-03-31 19:57:13 -04:00
|
|
|
{@a key-event}
|
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Key event filtering (with `key.enter`)
|
|
|
|
The `(keyup)` event handler hears *every keystroke*.
|
|
|
|
Sometimes only the _Enter_ key matters, because it signals that the user has finished typing.
|
|
|
|
One way to reduce the noise would be to examine every `$event.keyCode` and take action only when the key is _Enter_.
|
|
|
|
|
2017-04-26 08:11:02 -04:00
|
|
|
There's an easier way: bind to Angular's `keyup.enter` pseudo-event.
|
2017-02-22 13:09:39 -05:00
|
|
|
Then Angular calls the event handler only when the user presses _Enter_.
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/keyup.components.ts" region="key-up-component-3" header="src/app/keyup.components.ts (v3)"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
Here's how it works.
|
2017-03-30 15:04:18 -04:00
|
|
|
|
2019-11-11 17:47:51 -05:00
|
|
|
<div class="lightbox">
|
|
|
|
<img src='generated/images/guide/user-input/keyup3-anim.gif' alt="key up 3">
|
|
|
|
</div>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## On blur
|
|
|
|
|
|
|
|
In the previous example, the current state of the input box
|
|
|
|
is lost if the user mouses away and clicks elsewhere on the page
|
|
|
|
without first pressing _Enter_.
|
|
|
|
The component's `value` property is updated only when the user presses _Enter_.
|
|
|
|
|
|
|
|
To fix this issue, listen to both the _Enter_ key and the _blur_ event.
|
|
|
|
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/keyup.components.ts" region="key-up-component-4" header="src/app/keyup.components.ts (v4)"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Put it all together
|
2020-09-17 12:39:51 -04:00
|
|
|
|
|
|
|
This page demonstrated several event binding techniques.
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
Now, put it all together in a micro-app
|
|
|
|
that can display a list of heroes and add new heroes to the list.
|
|
|
|
The user can add a hero by typing the hero's name in the input box and
|
|
|
|
clicking **Add**.
|
|
|
|
|
2017-03-30 15:04:18 -04:00
|
|
|
|
2019-11-11 17:47:51 -05:00
|
|
|
<div class="lightbox">
|
|
|
|
<img src='generated/images/guide/user-input/little-tour-anim.gif' alt="Little Tour of Heroes">
|
|
|
|
</div>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
Below is the "Little Tour of Heroes" component.
|
|
|
|
|
|
|
|
|
2019-07-20 13:40:17 -04:00
|
|
|
<code-example path="user-input/src/app/little-tour.component.ts" region="little-tour" header="src/app/little-tour.component.ts"></code-example>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
### Observations
|
|
|
|
|
|
|
|
* **Use template variables to refer to elements** —
|
|
|
|
The `newHero` template variable refers to the `<input>` element.
|
|
|
|
You can reference `newHero` from any sibling or child of the `<input>` element.
|
|
|
|
|
|
|
|
* **Pass values, not elements** —
|
|
|
|
Instead of passing the `newHero` into the component's `addHero` method,
|
|
|
|
get the input box value and pass *that* to `addHero`.
|
|
|
|
|
|
|
|
* **Keep template statements simple** —
|
|
|
|
The `(blur)` event is bound to two JavaScript statements.
|
2019-10-26 10:35:03 -04:00
|
|
|
The first statement calls `addHero`. The second statement, `newHero.value=''`,
|
2017-02-22 13:09:39 -05:00
|
|
|
clears the input box after a new hero is added to the list.
|
|
|
|
|
2017-03-31 19:57:13 -04:00
|
|
|
|
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Source code
|
|
|
|
|
|
|
|
Following is all the code discussed in this page.
|
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
<code-tabs>
|
|
|
|
|
2018-10-11 07:29:59 -04:00
|
|
|
<code-pane header="click-me.component.ts" path="user-input/src/app/click-me.component.ts">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
</code-pane>
|
|
|
|
|
2018-10-11 07:29:59 -04:00
|
|
|
<code-pane header="keyup.components.ts" path="user-input/src/app/keyup.components.ts">
|
2017-03-27 11:08:53 -04:00
|
|
|
|
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2018-10-11 07:29:59 -04:00
|
|
|
<code-pane header="loop-back.component.ts" path="user-input/src/app/loop-back.component.ts">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2018-10-11 07:29:59 -04:00
|
|
|
<code-pane header="little-tour.component.ts" path="user-input/src/app/little-tour.component.ts">
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
</code-pane>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
2017-03-27 11:08:53 -04:00
|
|
|
</code-tabs>
|
2017-02-22 13:09:39 -05:00
|
|
|
|
|
|
|
|
2020-02-21 20:56:25 -05:00
|
|
|
Angular also supports passive event listeners. For example, you can use the following steps to make the scroll event passive.
|
2017-03-31 19:57:13 -04:00
|
|
|
|
2020-02-21 20:56:25 -05:00
|
|
|
1. Create a file `zone-flags.ts` under `src` directory.
|
|
|
|
2. Add the following line into this file.
|
|
|
|
|
|
|
|
```
|
|
|
|
(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`.
|
|
|
|
|
|
|
|
```
|
|
|
|
import './zone-flags';
|
2021-02-15 04:08:30 -05:00
|
|
|
import 'zone.js'; // Included with Angular CLI.
|
2020-02-21 20:56:25 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
After those steps, if you add event listeners for the `scroll` event, the listeners will be `passive`.
|
2017-03-31 19:57:13 -04:00
|
|
|
|
2017-02-22 13:09:39 -05:00
|
|
|
## Summary
|
|
|
|
|
|
|
|
You have mastered the basic primitives for responding to user input and gestures.
|
|
|
|
|
|
|
|
These techniques are useful for small-scale demonstrations, but they
|
|
|
|
quickly become verbose and clumsy when handling large amounts of user input.
|
|
|
|
Two-way data binding is a more elegant and compact way to move
|
|
|
|
values between data entry fields and model properties.
|
2020-09-10 18:52:12 -04:00
|
|
|
The [`Forms`](guide/forms-overview) page explains how to write
|
2017-04-26 08:11:02 -04:00
|
|
|
two-way bindings with `NgModel`.
|