1.6. Angular 2 user input

发布时间 :2025-10-25 12:21:06 UTC      

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:

Image0

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

1.6.1. 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!';
  }
}

1.6.2. 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.

1.6.3. 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 + ' | ';
  }
}

1.6.4. 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 = '';
}

1.6.5. 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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.