Event Binding in Angular
https://www.pluralsight.com/guides/angular-event-binding
Introduction
In this guide, we will explore on the topic of event binding in Angular. Event binding will help to build interactive web applications with the flow of data from component to the element and from element to component - both ends.
In many cases, users will not only just view the information or data on web applications or mobile applications, but will also interact with these applications using different user actions like clicks, keystrokes, change events, etc.
Event binding syntax will have a target event name within parentheses on the left of an equal sign, and a quoted template statement on the right.
Syntax: (event)
Let's consider an example where we are binding an onClick() event to the button element. When the user clicks on the button, event binding listens to the button's click event and calls the component's onClick() method.
File Name: example.component.ts
import { Component } from "@angular/core"; @Component({
selector: 'app-example',
template: `
<div>
<button (click)="onClick()">Click me!</button>
</div>
`
})
export class ExampleComponent {
onClick(){
alert("You Clicked Me!");
}
}
Below are some of the different ways and scenarios of event binding.
Target Event Binding
The target event is identified by the name within the parenthesis, ex: (click), which represents the click event. In the example, above we saw the target click event bound to the 'onClick()' method, which will listen to the button's click event.
1
<button (click) = "onClick()">Click me!</button>
We can also use the prefix on-, in event binding this is known as canonical form.
1
<button on-click = "onClick()">Click me!</button>
If the name of the target event does not match with the element's event, then Angular will throw an error "unknown directive".
$event Handling and Event Handling Statements
In event binding, we are binding an event handler for the target event. Whenever we perform some operations, an event will be raised. The event handler then executes the template statement. The template handler will have a receiver, which will perform the operation based on the event received and then respond. One such response would be storing a value from view to an array in the component.
If the event is a native DOM element event then the $event is a DOM element object with different properties like target and target.value.
File Name: example.component.ts
import { Component } from "@angular/core";
import { Person } from '../person'; @Component({
selector: 'app-example',
template: `
<div>
<input [value]="person.name"
(input)="person.name=$event.target.value" >
</div>
`
})
export class ExampleComponent {
person: Person;
}
In the above example we can see 'person.name' bound to $event.target.value.
Binding Custom Events with EventEmitter
Syntax: EventEmitter.emit(payload)
We can create custom events using EventEmitter and expose their properties. We can fire an event by calling the EventEmitter.emit(payload) passing payload which can contain any value. The parent, or the component to which we are passing the value, will listen for the event by binding to this property and accessing the payload through the $event object.
Let's consider an example where we have an ExampleComponent that presents ‘person’ information and responds to user actions. Although the ExampleComponent has a click button, it doesn't know how to click the person itself. The best it can do is raise an event reporting the user's click request.
File Name: example.component.ts
import { Component } from "@angular/core";
import { Person } from '../person'; @Component({
selector: 'app-example',
template: `
<img src="{{personImageUrl}}">
<span [style.text-decoration]="lineThrough">
{{prefix}} {{person?.name}}
</span>
<button (click)="onClick()">Click me!</button>
</div>
`
})
export class ExampleComponent {
clickRequest = new EventEmitter<Person>(); onClick() {
this.clickRequest.emit(this.person);
}
}
In the example above, component defines a clickRequest property that returns an EventEmitter. When the user clicks the 'Click me!' button, the component invokes the onClick() method, telling the EventEmitter to emit a Person object.
Now, let's consider a parent component that binds to the ExampleComponent's clickRequest event.
1
<app-person-details (clickRequest)="clickPerson($event)" [person]="personName"></app-person-details>
When the clickRequest event fires, Angular calls the parent component's clickPerson method, passing the person-to-click (emitted by PesonDetail) in the $event variable.
Conclusion
In this guide, we have explored the Event Binding technique in Angular. We have also seen different methods or ways through which we can bind an event to a DOM element.
As we know two-way data binding involves both the property binding and the event binding. You can learn about two-way data binding in my guide One-way and Two-way Data Binding in Angular.
Event Binding in Angular的更多相关文章
- Daikon Forge GUI Library(dfgui)之Event Binding
点击按钮并弹出对话框,就用下面的大问题按钮吧 1,选中按钮,Component/Daikon Forge/Data Binding/Event Binding 2,UI上创建DfPanel,并将其Be ...
- angular学习(二)—— Data Binding
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...
- ANGULAR 2 FOR REACT DEVELOPERS
Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...
- Angular学习笔记(2)——TODO小应用
Angular学习笔记(2)--TODO小应用 1. 写在前面 之前我们跑了Angular的Hello World,你是不是对它有点感觉了呢?这一篇将结合一个TODO程序来继续学习Angular的用法 ...
- 【Angular 5】数据绑定、事件绑定和双向绑定
本文为Angular5的学习笔记,IDE使用Visual Studio Code,内容是关于数据绑定,包括Property Binding.Class Binding.Style Binding. 在 ...
- angular学习3
#创建了一个component 查看angular.json文件: "prefix":"app", 在所创建的component的selector上添加了app ...
- Angular 2 Architecture Overview
Module 简单来说模块(module)就是完成共同目的的代码块,export一些内容例如一个类.函数.或值变量. component就是一个基本的Angular块,一个component类其实也是 ...
- Angular语法(三)——数据绑定
绑定类型 绑定类型可以按照数据流的方向分为三类:从源到视图,从视图到源,以及双向序列 示例 <!-- Bind button disabled state to `isUnchanged` pr ...
- [DOM Event Learning] Section 3 jQuery事件处理基础 on(), off()和one()方法使用
[DOM Event Learning] Section 3 jQuery事件处理基础 on(),off()和one()方法使用 jQuery提供了简单的方法来向选择器(对应页面上的元素)绑定事件 ...
随机推荐
- Git速成学习第六课:Bug分支
Git速成学习笔记整理于廖雪峰老师的官网网站:https://www.liaoxuefeng.com/ 当你接到一个修复代码为101的任务的时候,很自然的你想创建一个分支issue-101来修复它,但 ...
- shell学习笔记3-后台执行命令
cron 系统调度进程,使用它在每天的某时间或一周.一月的不同时间运行 at 使用它在一个特点的时间允许一些特殊的作业 & 使用它在后台允许一个占用时间不长的进程 Nohup 使用它在后台运行 ...
- OpenCV3编程入门.记录
ZC:OpenCV3编程入门_毛星云编著_电子工业出版.pdf 1.在看到 PDF.P134(计算数组加权和:addWeighted()函数)的时候,其中讲到“当输出数组的深度为CV_32S时,这个函 ...
- elasticsearch基本概念理解+elasticsearch 的shards unassigned处理方法 -- 最佳运维实践 - 集群规划
1.es与MySQL的概念对比 2.概念理解 2.1 Index : 一个索引即是文档的集合 2.2 Document : 一个文档即是一个可被索引的基础单元信息,一条记录: 2.3 Replicas ...
- 生成SSH秘钥连接github(详细教程)
第一步.首先,检查下自己之前有没有已经生成: 在开始菜单中打开git下的git bash(当然,在其他目录下打开git bash也是一样的): 然后执行 cd ~/.ssh 第二步.如果能进入到.ss ...
- 洛谷 题解 CF903B 【The Modcrab】
如果它在接下来一回合能一下就将你KO了,那么,你就十分需要回血(一直回到它一下敲不死你),否则你就一直打它就好了. #include<iostream> using namespace s ...
- PTA(Advanced Level)1044.Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- HanLP封装为web services服务的过程介绍
前几天的召开的2019年大数据生态产业大会不知道大家关注到没有,看到消息是hanlp2.0版本发布了.不知道hanlp2.0版本又将带来哪些新的变化?准备近期看能够拿到一些hanlp2.0的资料,如果 ...
- redis集群(多机)分布
一.实现原理 一致性哈希算法(Consistent Hashing): http://www.zsythink.net/archives/1182 二.配置两个redis服务,端口号要不一致 三.代码 ...
- (MVC — — Demo)客户管理系统的开发日志
点击一下 目录 第一步:搭建开发环境 第二步:层次包(按照三层架构思想写) 第四步:开发(utils)工具包 第四步:开发 Dao 层 第五步:开发 services 层 第六步:开发 factory ...