Runoob Tutorial ArchiveEnglish

Angular 2 User Input

Angular 2 User Input

When users click links, press buttons, or type text, these user interaction behaviors will trigger DOM events.

In this chapter, we will learn how to use Angular event binding syntax to bind these events.

The following Gif demonstrates the operation of this example:

angular 1

The source code can be downloaded at the end of the article.


Binding to User Input Events

We can use Angular's event binding mechanism to respond to any DOM event.

The following examples will bind a click event:

Original prompt (Chinese, left untranslated)



<button (click)="onClickMe()">点我!</button>

The (click) on the left side of the equals sign indicates that the click event of this button is the binding target. The text in quotes on the right side of the equals sign is a template string.

The complete code is as follows:

app/click-me.component.ts file:

import

{

Component

}

from

'

@angular/core

'

; @

Component

(

{

selector

:

'

click-me

'

,

template

: ` <

button

(

click

)

=

"

onClickMe()

"

Click me!

button

{

{

clickMessage

}

}

`

}

)

export

class

ClickMeComponent

{

clickMessage

=

'

'

;

onClickMe

(

)

{

this

.

clickMessage

=

'

Beginner Tutorial!

'

;

}

}


Get user input through the $event object

We can bind to all types of events.

Let's try binding to a keyup event of an input box and displaying the user's input on the screen.

app/keyup.component.ts (v1) file:

@

Component

(

{

selector

:

'

key-up1

'

,

template

: <

input

(

keyup

)

=

"

onKey($event)

"

<p{{values}}>
</p`})export class KeyUpComponent_v1{values=''>

;

/*

// non-strongly-typed onKey(event:any) { this.values += event.target.value + ' | '; }

*/

//

Strong typing

onKey

(

event

:

KeyboardEvent

)

{

this

.

values

+=

(

<

HTMLInputElement

event

.

target

)

.

value

'

|

'

;

}

}

In the code above, we listen to an event and capture user input, and Angular stores the event object in the $event variable.

The component's onKey() method is used to extract the user input from the event object and then accumulate the input value into the values property.


Get user input from a template reference variable

You can display user data by using local template variables, and template references are achieved by adding a hash symbol (#) before the identifier.

The following example demonstrates how to use local template variables:

app/loop-back.component.ts file:

@

Component

(

{

selector

:

'

loop-back

'

,

template

: ` <

input

box

(

keyup

)

=

"

0

"

<p{{box>

.

value

}

}

</

p

`

}

)

export

class

LoopbackComponent

{

}

We define a template reference variable named<input>on the element.boxThe variable refers toboxthe element itself, which means we can get the input element's<input>The element itself, meaning we can get the input element'svaluevalue, and display it in the<p>tag using an interpolation expression.

We can use template references to modify the above keyup instance:

app/keyup.components.ts (v2) file:

@

Component

(

{

selector

:

'

key-up2

'

,

template

: ` <

input

box

(

keyup

)

=

"

onKey(box.value)

"

<p{{values}}>
</p`})export class KeyUpComponent_v2{values=''>

;

onKey

(

value

:

string

)

{

this

.

values

+=

value

'

|

'

;

}

}


Key event filtering (via key.enter)

We can only get the input box value when the user presses the Enter key.

The (keyup) event handler will listen to every key press; we can filter the keys, for example, every $event.keyCode, and only update the values property when the Enter key is pressed.

Angular can filter keyboard events for us by binding to Angular's keyup.enter pseudo-event to listen for the Enter key.

app/keyup.components.ts (v3):

@

Component

(

{

selector

:

'

key-up3

'

,

template

: <

input

box

(

keyup

.

enter

)

=

"

values=box.value

"

<p{{values}}>
</p`})export class KeyUpComponent_v3{values=''>

;

}


blur (lose focus) event

Next, we can use the blur (lose focus) event, which can update the values attribute after an element loses focus.

The following example simultaneously listens for the Enter key press and the input box losing focus events.

app/keyup.components.ts (v4):

@

Component

(

{

selector

:

'

key-up4

'

,

template

: ` <

input

box

(

keyup

.

enter

)

=

"

values=box.value

"

(

blur

)

=

"

values=box.value

"

<p{{values}}>
</p`})export class KeyUpComponent_v4{values=''>

;

}

The source code used in this article can be downloaded in the following way, excluding the node_modules and typings directories.

Download source code