欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular 7工作方式事件綁定

 更新時(shí)間:2023年12月08日 10:59:11   作者:無(wú)涯教程  
在本章中將討論事件綁定在Angular7中的工作方式,當(dāng)用戶以鍵盤移動(dòng),鼠標(biāo)單擊或鼠標(biāo)懸停的形式與應(yīng)用程序交互時(shí),它將生成一個(gè)事件,需要處理這些事件以執(zhí)行某種操作,考慮一個(gè)示例以更好地理解這一點(diǎn)

Angular 7工作方式事件綁定

當(dāng)用戶以鍵盤移動(dòng),鼠標(biāo)單擊或鼠標(biāo)懸停的形式與應(yīng)用程序交互時(shí),它將生成一個(gè)事件,需要處理這些事件以執(zhí)行某種操作,考慮一個(gè)示例以更好地理解這一點(diǎn)

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 文件中,無(wú)涯教程定義了一個(gè)按鈕,并使用click事件為其添加了一個(gè)函數(shù)。

以下是定義按鈕并為其添加函數(shù)的語(yǔ)法。

(click)="myClickFunction($event)"

該函數(shù)在: 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 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);
   }
}

單擊按鈕后,控件將轉(zhuǎn)到函數(shù) myClickFunction ,然后將出現(xiàn)一個(gè)對(duì)話框,其中顯示已單擊按鈕,如以下屏幕截圖所示-

按鈕的樣式

添加在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;
}

change事件添加

將onchange事件添加到下拉列表中,以下代碼行將幫助您將change事件添加到下拉列表中

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 (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 文件中聲明

該函數(shù)在 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 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);
   }
}

從下拉列表中選擇月份,您會(huì)在控制臺(tái)中看到控制臺(tái)消息" Changed month from the Dropdown"以及事件。

當(dāng)下拉列表中的值更改時(shí),讓無(wú)涯教程在 app.component.ts 中添加警報(bào)消息,如下所示-

import { Component } from '@angular/core';
@Component({ 
   selector: 'app-root', 
   templateUrl: './app.component.html', 
   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");
   } 
}

更改下拉列表中的值時(shí),將出現(xiàn)一個(gè)對(duì)話框,并顯示以下消息:

"Changed month from the Dropdown"。

以上就是Angular 7工作方式事件綁定的詳細(xì)內(nèi)容,更多關(guān)于Angular7事件綁定的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論