Angular 6 - 事件繫結



在本節中,我們將討論 Angular 6 中事件繫結的工作原理。當用戶以鍵盤移動、滑鼠點選或滑鼠懸停的形式與應用程式互動時,它會生成事件。需要處理這些事件以執行某種操作。這就是事件繫結發揮作用的地方。

我們來看一個例子來更好地理解這一點。

app.component.html

<!--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.component.html 檔案中,我們定義了一個按鈕,並使用 click 事件向其添加了一個函式。

以下是定義按鈕並向其新增函式的語法。

(click)="myClickFunction($event)"

該函式在 .ts 檔案中定義: app.component.ts

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'Angular 6 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,並且將出現一個對話方塊,顯示 Button is clicked,如下圖所示 −

Output Using myClickFunction

現在讓我們向下拉列表新增 change 事件。

以下程式碼行將幫助你向下拉列表新增 change 事件 −

<!--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.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'Angular 6 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”會與事件一起顯示在控制檯中。

Changed Month From Dropdown

讓我們在 app.component.ts 中新增一個警報訊息,如下所示,當從下拉列表更改值時顯示該訊息 −

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'Angular 6 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");
   }
}

當下拉列表中的值更改時,將出現一個對話方塊,並顯示以下訊息 - “Changed month from the Dropdown”。

Changed Month From Dropdown2
廣告
© . All rights reserved.