首页 > 其他分享 >无涯教程-Angular7 - 事件绑定

无涯教程-Angular7 - 事件绑定

时间:2023-12-08 10:32:32浏览次数:43  
标签:console log app 绑定 component 无涯 Angular7 html event

在本章中,无涯教程将讨论事件绑定在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 ,然后将出现一个对话框,其中显示已单击按钮,如以下屏幕截图所示-

Click

按钮的样式添加在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;
}

现在让无涯教程将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 文件中声明-

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"以及事件。

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

Condition

参考链接

https://www.learnfk.com/angular7/angular7-event-binding.html

标签:console,log,app,绑定,component,无涯,Angular7,html,event
From: https://blog.51cto.com/u_14033984/8733584

相关文章

  • WPF 绑定binding都有哪些事件
    在WPF中,源属性(SourceProperty)指的是提供数据的属性,通常是数据模型或者其他控件的属性,而目标属性(TargetProperty)则是数据绑定的目标,通常是绑定到控件的属性,例如TextBlock的Text属性。数据绑定将源属性的值自动更新到目标属性中。 主要包含以下几个事件:1. UpdateSourceTrigg......
  • 无涯教程-Angular7 - 简介
    Angular7是一个开放源代码JavaScript框架,用于在JavaScript, html 和Typescript(JavaScript的超集)中构建Web应用程序和应用程序。Angular为animation,httpservice和materials提供了内置功能,这些功能又具有auto-complete,naigation,toolbar,menus等功能。代码以Typescript编写......
  • 无涯教程-Erlang - is_alive函数
    如果本地节点处于活动状态并且可以是分布式系统的一部分,则返回true。否则,它返回false。is_alive-语法is_alive()is_alive-返回值如果本地节点处于活动状态并且可以是分布式系统的一部分,则返回true。否则,它返回false。-module(helloLearnfk).-export([start/0]).sta......
  • 无涯教程-Erlang - spawn函数
    这用于创建新进程并对其进行初始化。spawn-语法spawn(Function)Function - 需要产生的功能。spawn-返回值此方法返回一个进程ID。-module(helloLearnfk).-export([start/0]).start()->spawn(fun()->server("Hello")end).server(Message)->io:f......
  • 无涯教程-Erlang - unregister函数
    这用于注销系统中的进程。unregister-语法unregister(atom)atom-这是要赋予该过程的注册名称。unregister-示例-module(helloLearnfk).-export([start/0,call/2]).call(Arg1,Arg2)->io:fwrite("~p~n",[Arg1]).start()->Pid=spawn(?MODULE,cal......
  • 无涯教程-Erlang - register函数
    这用于在系统中注册进程。register-语法register(atom,pid)atom-这是要赋予该过程的注册名称。pid  -这是需要绑定到原子的进程ID。register-示例-module(helloLearnfk).-export([start/0,call/2]).call(Arg1,Arg2)->io:fwrite("~p~n",[Arg1]).......
  • 无涯教程-Erlang - is_pid函数
    此方法用于确定进程ID是否存在。is_pid-语法Is_pid(processid)processid  - 这是需要检查的进程ID,是否存在。is_pid-返回值如果进程ID存在,则返回true,否则将返回false。-module(helloLearnfk).-export([start/0,call/2]).call(Arg1,Arg2)->io:format("......
  • 可调用对象包装器和绑定器
    文章参考:爱编程的大丙(subingwen.cn)1.可调用对象一组执行任务的语句都可以视作一个函数、一个可调用对象。C++中提供了可调用对象的概念,其应用相当广泛,例如在处理一些回调函数、触发函数时就会用到。可调用对象有如下几种类型:函数指针:intadd(inta,intb){ret......
  • 无涯教程-Erlang - binary_to_list函数
    此方法用于将二进制值转换为列表。binary_to_list-语法binary_to_list(binaryvalue)binaryvalue- 这是需要转换为列表的二进制值。binary_to_list-返回值返回列表。-module(helloLearnfk).-export([start/0]).start()->io:fwrite("~p~n",[binary_to_lis......
  • 无涯教程-Erlang - is_binary函数
    此方法用于检查位串是否确实是二进制值。is_binary-语法is_binary(bitstring)bitstring-这是需要检查其是否为二进制的位串。is_binary-返回值如果位串是二进制值,则返回true;否则返回false。-module(helloLearnfk).-export([start/0]).start()->io:fwrite("......