Angular 7工作方式事件绑定

 更新时间:2023年12月08日 10:59:11   作者:无涯教程  
在本章中将讨论事件绑定在Angular7中的工作方式,当用户以键盘移动,鼠标单击或鼠标悬停的形式与应用程序交互时,它将生成一个事件,需要处理这些事件以执行某种操作,考虑一个示例以更好地理解这一点

Angular 7工作方式事件绑定

当用户以键盘移动,鼠标单击或鼠标悬停的形式与应用程序交互时,它将生成一个事件,需要处理这些事件以执行某种操作,考虑一个示例以更好地理解这一点

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 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);
   }
}

单击按钮后,控件将转到函数 myClickFunction ,然后将出现一个对话框,其中显示已单击按钮,如以下屏幕截图所示-

按钮的样式

添加在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 文件中声明

该函数在 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);
   }
}

从下拉列表中选择月份,您会在控制台中看到控制台消息" Changed month from the 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 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");
   } 
}

更改下拉列表中的值时,将出现一个对话框,并显示以下消息:

"Changed month from the Dropdown"。

以上就是Angular 7工作方式事件绑定的详细内容,更多关于Angular7事件绑定的资料请关注脚本之家其它相关文章!

相关文章

  • AngularJS使用angular.bootstrap完成模块手动加载的方法分析

    AngularJS使用angular.bootstrap完成模块手动加载的方法分析

    这篇文章主要介绍了AngularJS使用angular.bootstrap完成模块手动加载的方法,结合实例形式分析了angular.bootstrap函数手动加载模块的步骤与相关操作技巧,需要的朋友可以参考下
    2017-01-01
  • AngularJS中$http的交互问题

    AngularJS中$http的交互问题

    本篇文章主要介绍了AngularJS中$http的交互问题 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • 基于angular中的重要指令详解($eval,$parse和$compile)

    基于angular中的重要指令详解($eval,$parse和$compile)

    下面小编就为大家带来一篇基于angular中的重要指令详解($eval,$parse和$compile)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • angular 用拦截器统一处理http请求和响应的方法

    angular 用拦截器统一处理http请求和响应的方法

    下面小编就为大家带来一篇angular 用拦截器统一处理http请求和响应的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • angular2 NgModel模块的具体使用方法

    angular2 NgModel模块的具体使用方法

    这篇文章主要介绍了angular2 NgModel模块的具体使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • AngularJS深入探讨scope,继承结构,事件系统和生命周期

    AngularJS深入探讨scope,继承结构,事件系统和生命周期

    这篇文章主要介绍了AngularJS的scope,继承结构,事件系统和生命周期,较为详细的分析了scope的作用域、层次结构、继承及生命周期相关概念与使用技巧,需要的朋友可以参考下
    2016-11-11
  • Angular.js中处理页面闪烁的方法详解

    Angular.js中处理页面闪烁的方法详解

    我们在应用的页面或者组件需要加载数据时,浏览器和angular渲染页面都需要消耗一定的时间。这里的间隔可能很小,甚至让人感觉不到区别;但也可能很长,这样会导致让我们的用户看到了没有被渲染过的页面。本文将介绍Angular.js中处理页面闪烁的方法。
    2017-03-03
  • angular实现图片懒加载实例代码

    angular实现图片懒加载实例代码

    本篇文章主要介绍了angular实现图片懒加载实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • AngularJs实现分页功能不带省略号的代码

    AngularJs实现分页功能不带省略号的代码

    这篇文章主要介绍了AngularJs实现分页功能不带省略号的代码的相关资料,非常不错具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-05-05
  • 在AngularJS应用中实现一些动画效果的代码

    在AngularJS应用中实现一些动画效果的代码

    这篇文章主要介绍了在AngularJS应用中实现一些动画效果的代码,AngularJS是一款热门的JavaScript库,需要的朋友可以参考下
    2015-06-06

最新评论