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!");
}
}
typeScript

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>
html

We can also use the prefix on-, in event binding this is known as canonical form.

1
<button on-click = "onClick()">Click me!</button>
html

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

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

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>
html

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的更多相关文章

  1. Daikon Forge GUI Library(dfgui)之Event Binding

    点击按钮并弹出对话框,就用下面的大问题按钮吧 1,选中按钮,Component/Daikon Forge/Data Binding/Event Binding 2,UI上创建DfPanel,并将其Be ...

  2. angular学习(二)—— Data Binding

    转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...

  3. 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, ...

  4. Angular学习笔记(2)——TODO小应用

    Angular学习笔记(2)--TODO小应用 1. 写在前面 之前我们跑了Angular的Hello World,你是不是对它有点感觉了呢?这一篇将结合一个TODO程序来继续学习Angular的用法 ...

  5. 【Angular 5】数据绑定、事件绑定和双向绑定

    本文为Angular5的学习笔记,IDE使用Visual Studio Code,内容是关于数据绑定,包括Property Binding.Class Binding.Style Binding. 在 ...

  6. angular学习3

    #创建了一个component 查看angular.json文件: "prefix":"app", 在所创建的component的selector上添加了app ...

  7. Angular 2 Architecture Overview

    Module 简单来说模块(module)就是完成共同目的的代码块,export一些内容例如一个类.函数.或值变量. component就是一个基本的Angular块,一个component类其实也是 ...

  8. Angular语法(三)——数据绑定

    绑定类型 绑定类型可以按照数据流的方向分为三类:从源到视图,从视图到源,以及双向序列 示例 <!-- Bind button disabled state to `isUnchanged` pr ...

  9. [DOM Event Learning] Section 3 jQuery事件处理基础 on(), off()和one()方法使用

    [DOM Event Learning] Section 3 jQuery事件处理基础 on(),off()和one()方法使用   jQuery提供了简单的方法来向选择器(对应页面上的元素)绑定事件 ...

随机推荐

  1. libvirt2.0安装

    目录 1.libvirt介绍 2.卸载系统自带的libvirt 2.1.查看当前安装的libvirt相关包 2.2.全部卸载掉 3.使用tar包编译安装 3.1.解压缩 3.2.生成Makefile文 ...

  2. Java学习笔记-Java概述和环境配置

    基础常识 软件:一系列按照特定顺序组织的计算机数据 和指令的集合 常见的软件: 系统软件:如:DOS,windows,Linux等 应用软件:如:扫雷,迅雷,QQ等 软件的出现实现了人与计算机之间的更 ...

  3. ubuntu配置vnc服务

    今晚比较闲,就用ubuntu系统搭了vnc系统,真的好用(比centos简单多了). 简单介绍下,VNC(Virtual Network Computing)服务是一款优秀的屏幕分享及远程连接服务,基 ...

  4. chrome 监听touch类事件报错:无法被动侦听事件preventDefault

    先上错误信息: Unable to preventDefault inside passive event listener due to target being treated as passiv ...

  5. LeetCode. 矩阵中的最长递增路径

    题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...

  6. DP+线段树维护矩阵(2019牛客暑期多校训练营(第二场))--MAZE

    题意:https://ac.nowcoder.com/acm/contest/882/E 给你01矩阵,有两种操作:1是把一个位置0变1.1变0,2是问你从第一行i开始,到最后一行j有几种走法.你只能 ...

  7. [游戏复刻] 2048(2014. Android)

    等哪一天我有很多很多的时间再写吧...

  8. Django-djangorestframework-请求模块-获取请求参数

    目录 请求模块 源码分析 正式使用 总结 请求模块 主要是分析 drf 二次封装后的 request 对象 以及怎么拿到请求传递过来的数据(url 拼接的数据,数据包传过来的数据) 源码分析 源码查看 ...

  9. 如何找到程序的真正入口mainCRTStartup

    相信大家都知道以为程序的入口为main函数,但是程序的真正的入口不是main而是mainCRTStartup,那么我们如何找到他的地址呢? 先用第一种方法,就是直接代码显示 #include<s ...

  10. hdu 3500 还是搜索

    这道题目由于每走一步的时候毛毛球是可以变换的 换言之 主体不唯一 所以这里搜索的设计有变化 再就是几个回溯的过程要注意.,.  小心使得万年船 #include <iostream> #i ...