Angular 7工作方式事件綁定
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)文章
Angular2中constructor和ngOninit的使用講解
這篇文章主要介紹了Angular2中constructor和ngOninit的使用講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05AngularJs 國(guó)際化(I18n/L10n)詳解
本文主要介紹AngularJs 國(guó)際化的知識(shí),這里整理了詳細(xì)的資料來講解國(guó)際化,有需要的小伙伴可以參考下2016-09-09Angular.js中用ng-repeat-start實(shí)現(xiàn)自定義顯示
大家都知道Angular.js可以用ng-repeat來顯示列表數(shù)據(jù),可是如果想要自定義顯示數(shù)據(jù)列表的話ng-repeat就實(shí)現(xiàn)不了了,這個(gè)時(shí)候可以利用ng-repeat-start 和 ng-repeat-end來實(shí)現(xiàn),下面通過本文來詳細(xì)看看實(shí)現(xiàn)的方法吧。2016-10-10AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法
這篇文章主要介紹了AngularJS基于provider實(shí)現(xiàn)全局變量的讀取和賦值方法,結(jié)合實(shí)例形式分析了AngularJS全局變量的聲明、賦值、讀取等相關(guān)使用技巧,需要的朋友可以參考下2017-06-06AngularJS API之copy深拷貝詳解及實(shí)例
這篇文章主要介紹了AngularJS API之copy深拷貝詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果
本篇文章主要介紹了Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01