Angular7-event-binding
提供:Dev Guides
Angular7-イベントバインディング
この章では、Angular 7でイベントバインディングがどのように機能するかについて説明します。 ユーザーがキーボードの動き、マウスのクリック、またはマウスオーバーの形でアプリケーションと対話すると、イベントが生成されます。 これらのイベントは、何らかのアクションを実行するために処理する必要があります。 これは、イベントバインディングが登場する場所です。
これをよりよく理解するための例を考えてみましょう。
*_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)"
関数は: app.component.ts で定義されています
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.componentl',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Angular 7';
//declared array of months.
months = ["January", "February", "March", "April", "May","June", "July",
"August", "September", "October", "November", "December"];
isavailable = true;//variable is set to 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 に移動し、ダイアログボックスが表示されます。ダイアログボックスには、次のスクリーンショットに示すように、*ボタンがクリックされました*と表示されます-
ボタンのスタイリングはadd.component.cssに追加されます-
button {
background-color: #2B3BCF;
border: none;
color: white;
padding: 10px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
}
次に、onchangeイベントをドロップダウンに追加しましょう。
次のコード行は、ドロップダウンに変更イベントを追加するのに役立ちます-
*_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 (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>
<br/>
<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 7';
//declared array of months.
months = ["January", "Feburary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
isavailable = true;//variable is set to 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) {
console.log("Changed month from the Dropdown");
console.log(event);
}
}
ドロップダウンから月を選択すると、「ドロップダウンからの月が変更されました」というコンソールメッセージがイベントとともにコンソールに表示されます。
以下に示すように、ドロップダウンからの値が変更されたときに、 app.component.ts にアラートメッセージを追加しましょう-
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.componentl',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Angular 7';
//declared array of months.
months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
isavailable = true;//variable is set to 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");
}
}
ドロップダウンの値が変更されると、ダイアログボックスが表示され、次のメッセージが表示されます-
「ドロップダウンから月を変更」。