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提供了简单的方法来向选择器(对应页面上的元素)绑定事件 ...
随机推荐
- 字符编码笔记:ASCII,Unicode 和 UTF-8(理解)
1.ASCII 码 美国制定的字符编码规则,对英语字符与二进制位之间的关系做了统一规定. 占一个字节,8 位,最多可表示 2^8 = 256 种状态(字符) 实际共有 128 个字符,只占用一个字节的 ...
- 破周三,前不着村后不着店的,只好学pandas了,你该这么学,No.9
如果图片无法观看,请移步 https://blog.csdn.net/hihell 周三了,一个星期最难的一天 大中间的,今天还这么热 5月份,36度的高温 天空飘过几个字 屋里学pandas最得劲 ...
- 记:linux服务器启动重启WEB项目启动成功,长时间卡住未响应
问题 云服务器部署web项目,每次正常启动项目后访问页面很久才能有响应,一直卡在INFO: Deploying web application directory ......长达几分钟以上,极度影响 ...
- 1、spring mvc jsp页面中文乱码
jsp 页面头部 的page标签中加个 contentType="text/html;charset=utf-8"
- win7 配置 用户/系统DSN (解决plsql odbc导入器 DSN 没有选项)
(2013-05-07 15:07:29) 转载▼ 标签: it 老笔记本越来越卡,最近重新做了win7的系统 从sql server中导出的 excel文件 准备通过plsql倒入的oracle ...
- cook,session
//发送session request.getSession().setAttribute("user", "zs1"); //发送cookie //需要自动登 ...
- Idea中Smart Tomcat插件启动报NullPointerException问题
如果你跟我一样用的是Idea Community社区版的话,也一定会遇到用Smart Tomcat插件启动报错的问题: 这个问题网上搜了一圈,大家好像也都遇到过了,不过也都没有找到原因和给出解决方案. ...
- H5网页唤醒app,判断app安装
在阅读本文之前你首先应该对js有基本对掌握,并且对Scheme,intent有一定的理解.更多的是代码 上午给朋友做了一个产品引导页,但是需要判断ios系统的TestFlight是否安装,进行了goo ...
- 超级简单的requests模块教程
在web后台开发过程中,会遇到需要向第三方发送http请求的场景,python中的requests库可以很好的满足这一要求,这里简要记录一下requests模块的使用! 说明: 这里主要记录一下req ...
- Docker 镜像的制作
1.登录docker docker ecex –it 容器名/容器id /bin/bash 例如: docker exec –it t ...