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. Linux 之 awk文本分析工具

    AWK是一种处理文本文件的语言,是一个强大的文本分析工具.Linux环境中自带. awk调用方法 命令行 awk [-F field-separator] 'commands' input-file( ...

  2. vue学习中遇到的问题

    1.axios使用post传值时无法使用键值对传值的问题 问题的原因:主要是HTTP请求中的get请求和post请求参数的存放位置是不一样的,get请求的参数以键值对的方式跟在url后面的,而post ...

  3. 记录一次SignalR服务端实现过程

    前言:最近手上一个项目需要后端实时推送数据到前端,第一个想到的就是微软的SignalR,由于之前都是平时没事写的Demo,没有用到实际项目中,这次恰好用到了,因此记录下来整个实现过程(网上也有很多类似 ...

  4. nssm设置solr开机启动服务

    首先,下载nssm http://www.nssm.cc/download 命令 nssm install solr 然后到服务里启动solr,并设置为自动 Ctrl+Shift+Esc(说明:Esc ...

  5. Python基础总结之第十天开始【认识模块、包和库】(新手可相互督促)

    每天都有一种备课的赶脚~~~ 什么是模块? 在实际的开发过程中,代码量肯定有成千上万行的代码,甚至十几万行代码也很正常吧... 那么这么多的代码如果放在一个文件中,肯定是很不合适的,为了以后程序的编写 ...

  6. VS2013:error C1069: 无法读取编译器命令行

    前一阵搞python和matlab,没用VS 2013,今天打开一个C++程序想跑一跑,突然蹦出这么个错误,然后发现电脑上所有的程序都会这样了. 后来发现是TMP/TEMP环境变量路径有空格的问题,更 ...

  7. macos 更改罗技k810无线键盘的映射

    在mac系统中,command键非常关键,但k810接入后, win键被映射为Command,而Alt的位置却是mac内置键盘的Command的位置. 为方便使用,可以把Win键和Alt键做一个对换. ...

  8. android 自动化测试 ---python wrapper(python 包装)

    关于有道云笔记复制的东西不能直接copy到博客园,可以选择使用txt文件做个媒介 1.appium 2.monkeyrunner 3.uiautomator2 前面两种种方式都要加载androidsd ...

  9. Django的URLconf

    URL 概要 我们要在Django项目中为应用程序设计URL,我们可以创建一个名为URLconf(通常为urls.py)的Python模块.这个模块是纯Python代码,是一个简单的正则表达式到Pyt ...

  10. el-table el-column selection disable

    几个要点: 1.通过 selectable 绑定 2.绑定的方法只能返回0/1 <el-table-column type="selection" width="5 ...