Angular4-event-binding

提供:Dev Guides
移動先:案内検索

Angular 4-イベントバインディング

この章では、Angular 4でイベントバインディングがどのように機能するかについて説明します。 ユーザーがキーボードの動き、マウスのクリック、またはマウスオーバーの形でアプリケーションを操作すると、イベントが生成されます。 これらのイベントは、何らかのアクションを実行するために処理する必要があります。 これは、イベントバインディングが登場する場所です。

これをよりよく理解するための例を考えてみましょう。

app.componentl

<!--The content below is only a placeholder and can be replaced.-->
<div style = "text-align:center">
   <h1>
      Welcome to {{title}}.
   </h1>
</div>

<div> Months :
   <select>
      <option *ngFor = "let i of months">{{i}}</option>
   </select>
</div>
<br/>

<div>
   <span *ngIf = "isavailable; then condition1 else condition2">
      Condition is valid.
   </span>
   <ng-template #condition1>Condition is valid</ng-template>
   <ng-template #condition2>Condition is invalid</ng-template>
</div>
<button (click)="myClickFunction($event)">
   Click Me
</button>
*app.componentl* ファイルで、ボタンを定義し、クリックイベントを使用してボタンに関数を追加しました。

以下は、ボタンを定義し、ボタンに機能を追加する構文です。

(click)="myClickFunction($event)"

関数は .ts ファイルで定義されています: app.component.ts

import { Component } from '@angular/core';

@Component({
   selector: 'app-root',
   templateUrl: './app.componentl',
   styleUrls: ['./app.component.css']
})

export class AppComponent {
   title = 'Angular 4 Project!';
  //array of months.
   months = ["January", "Feburary", "March", "April",
            "May", "June", "July", "August", "September",
            "October", "November", "December"];
   isavailable = true;
   myClickFunction(event) {
     //just added console.log which will display the event details in browser on click of the button.
      alert("Button is clicked");
      console.log(event);
   }
}

ボタンをクリックすると、コントロールは関数 myClickFunction に移動し、次のスクリーンショットに示すように、ボタンがクリックされたことを示すダイアログボックスが表示されます-

myClickFunctionを使用した出力

変更イベントをドロップダウンに追加しましょう。

次のコード行は、ドロップダウンに変更イベントを追加するのに役立ちます-

<!--The content below is only a placeholder and can be replaced.-->
<div style = "text-align:center">
   <h1>
      Welcome to {{title}}.
   </h1>
</div>

<div> Months :
   <select (change) = "changemonths($event)">
      <option *ngFor = "let i of months">{{i}}</option>
   </select>
</div>
<br/>

<div>
   <span *ngIf = "isavailable; then condition1 else condition2">
      Condition is valid.
   </span>
   <ng-template #condition1>Condition is valid</ng-template>
   <ng-template #condition2>Condition is invalid</ng-template>
</div>

<button (click) = "myClickFunction($event)">Click Me</button>

関数は、 app.component.ts ファイルで宣言されています-

import { Component } from '@angular/core';

@Component({
   selector: 'app-root',
   templateUrl: './app.componentl',
   styleUrls: ['./app.component.css']
})

export class AppComponent {
   title = 'Angular 4 Project!';
  //array of months.
   months = ["January", "Feburary", "March", "April",
            "May", "June", "July", "August", "September",
            "October", "November", "December"];
   isavailable = true;
   myClickFunction(event) {
      alert("Button is clicked");
      console.log(event);
   }
   changemonths(event) {
      console.log("Changed month from the Dropdown");
      console.log(event);
   }
}

コンソールメッセージ「 Changed month from the Dropdown 」がイベントとともにコンソールに表示されます。

ドロップダウンから月を変更

以下に示すように、ドロップダウンからの値が変更されたときに、 app.component.ts にアラートメッセージを追加しましょう-

import { Component } from '@angular/core';

@Component({
   selector: 'app-root',
   templateUrl: './app.componentl',
   styleUrls: ['./app.component.css']
})

export class AppComponent {
   title = 'Angular 4 Project!';
  //array of months.
   months = ["January", "February", "March", "April",
            "May", "June", "July", "August", "September",
            "October", "November", "December"];

   isavailable = true;
   myClickFunction(event) {
     //just added console.log which will display the event details in browser
      on click of the button.
      alert("Button is clicked");
      console.log(event);
   }
   changemonths(event) {
      alert("Changed month from the Dropdown");
   }
}

ドロップダウンの値が変更されると、ダイアログボックスが表示され、次のメッセージが表示されます-「ドロップダウンから月を変更しました」。

Dropdown2から月を変更