亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Angular 7工作方式事件綁定

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

Angular 7工作方式事件綁定

當(dāng)用戶(hù)以鍵盤(pán)移動(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)文章

  • Angular2中constructor和ngOninit的使用講解

    Angular2中constructor和ngOninit的使用講解

    這篇文章主要介紹了Angular2中constructor和ngOninit的使用講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • AngularJs 國(guó)際化(I18n/L10n)詳解

    AngularJs 國(guó)際化(I18n/L10n)詳解

    本文主要介紹AngularJs 國(guó)際化的知識(shí),這里整理了詳細(xì)的資料來(lái)講解國(guó)際化,有需要的小伙伴可以參考下
    2016-09-09
  • 詳解支持Angular 2的表格控件

    詳解支持Angular 2的表格控件

    本文主要對(duì)支持Angular2的表格控件進(jìn)行詳細(xì)介紹。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Angularjs自定義指令Directive詳解

    Angularjs自定義指令Directive詳解

    Directive是一個(gè)非常棒的功能??梢詫?shí)現(xiàn)我們自義的的功能方法。下面通過(guò)實(shí)例代碼給大家介紹Angularjs自定義指令Directive相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2017-05-05
  • Angular.js中用ng-repeat-start實(shí)現(xiàn)自定義顯示

    Angular.js中用ng-repeat-start實(shí)現(xiàn)自定義顯示

    大家都知道Angular.js可以用ng-repeat來(lái)顯示列表數(shù)據(jù),可是如果想要自定義顯示數(shù)據(jù)列表的話ng-repeat就實(shí)現(xiàn)不了了,這個(gè)時(shí)候可以利用ng-repeat-start 和 ng-repeat-end來(lái)實(shí)現(xiàn),下面通過(guò)本文來(lái)詳細(xì)看看實(shí)現(xiàn)的方法吧。
    2016-10-10
  • AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法

    AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法

    這篇文章主要介紹了AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法,結(jié)合實(shí)例形式分析了AngularJS全局變量的聲明、賦值、讀取等相關(guān)使用技巧,需要的朋友可以參考下
    2017-06-06
  • AngularJS路由刪除#符號(hào)解決的辦法

    AngularJS路由刪除#符號(hào)解決的辦法

    這篇文章主要介紹了AngularJS路由刪除#符號(hào)解決的辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • AngularJS API之copy深拷貝詳解及實(shí)例

    AngularJS API之copy深拷貝詳解及實(shí)例

    這篇文章主要介紹了AngularJS API之copy深拷貝詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果

    Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果

    本篇文章主要介紹了Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • 深入講解AngularJS中的自定義指令的使用

    深入講解AngularJS中的自定義指令的使用

    這篇文章主要介紹了深入講解AngularJS中的自定義指令的使用,AngularJS是一款熱門(mén)的JavaScript開(kāi)發(fā)庫(kù),需要的朋友可以參考下
    2015-06-06

最新評(píng)論