Angular 2 user input
When a user clicks a link, presses a button, or enters text, the interaction of these users will be triggered. DOM
events.
In this chapter, we will learn how to bind these events using Angular event binding syntax.
The following Gif figure illustrates the operation of this example:
The source code can be downloaded at the end of the article.
Bind to user input event
We can use the Angular event binding mechanism to respond to any DOM event.
The following example binds the click event:
<button (click)="onClickMe()">Click me!</button>
The click to the left of the equal sign indicates that the click event of the button is taken as the binding target. To the right of the equal sign, the text in the quotation marks is a template statement
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 = 'Rookie Tutorial!';
}
}
Pass through $event
object to get user input
We can bind to all types of events.
Let’s try binding to an input box keyup
event and echo what the user typed back to 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 + ' | ';
}
*/
// strongly typed
onKey(event: KeyboardEvent) {
this.values += (<HTMLInputElement>event.target).value + ' | ';
}
}
In the above code, we listen for an event and capture user input, and Angular stores the event object in the $event
variable.
Component onKey()
method is used to extract user input from the event object and add the input value to the values
property.
Get user input from a template reference variable
You can display user data by using local template variables, and template reference variables are achieved by adding a pound sign (#) before the identifier.
The following example shows 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’re here <input>
Element defines a file named box
the template reference variable of the box
variable refers to the <input>
element itself, which means that we can get the input element’s value
value and display it in the <p>
tag.
We can use template reference variables to modify the above keyup
example:
app/keyup.components.ts
(V2) documents:
@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 + ' | ';
}
}
Keystroke event filtering (via key.enter
)
We can get the value of the input box only when the user presses the enter (enter) key.
(keyup) the event handling statement hears every keystroke, and we can filter the keystroke, such as every keystroke $event.keyCode
updated only when the enter key is pressed values
property.
Angular can filter keyboard events for us by binding to Angular’s keyup.enter
the pseudo event listens for the event of 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
(loss of focus) event
Next we can use the blur
(lose focus) event, which can be updated afterthe element loses focus values
property.
The following example listens for events that both the enter key and the input box lose focus.
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 ways, not including node_modules
and typings
catalogue.