开发学院

您的位置:首页>教程>正文

教程正文

Angular 4 事件绑定

Angular 4 事件绑定

  在本节,我们将讨论如何在Angular 4中进行事件绑定。当用户以键盘操作、鼠标点击或鼠标悬停的形式与应用程序交互时,它会生成一个事件,需要有对应的事件处理器来响应操作,这就是所谓的事件绑定。

  让我们通过例子来更好地理解事件绑定。

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)"

  函数在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 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,并出现一个对话框,显示点击按钮,如下图所示:

output_using_myclickfunction.jpg

  现在让我们将事件绑定给下拉列表中。

  以下代码行将帮助您将更改事件添加到下拉列表中:

<!--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 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”与事件一起显示在控制台中。

changed_month_from_dropdown.jpg

  当下拉列表中的值发生变化时,让我们在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 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");
   }
}

  当下拉列表中的值发生变化时,将出现一个对话框,并显示以下消息:“Changed month from the Dropdown”。

changed_month_from_dropdown2.jpg