[Angular Directive] 3. Handle Events with Angular 2 Directives
A @Directive
can also listen to events on their host element using @HostListener
. This allows you to add behaviors that react to user input and update or modify properties on the element without having to create a custom component.
- import {Directive, HostListener, HostBinding, Input} from '@angular/core';
- @Directive({
- selector: '[clickable]'
- })
- export class ClickableDirective {
- @Input('clickable') text;
- @HostBinding() get innerText() {
- if(this.text) {
- return this.text;
- }
- }
- @HostListener('click', ['$event']) onClick(event) {
- console.log(event); //MouseEvent
- this.text = event.clientX;
- }
- }
We can add HostListener on the host element, and get '$event' pass to our handler function 'onClick'. Inside the function we are able to change the innerText of the directive.
[Angular Directive] 3. Handle Events with Angular 2 Directives的更多相关文章
- [Angular Directive] 2. Add Inputs to Angular 2 Directives
The @Input decorator allows you to pass values into your @Directive so that you can change the value ...
- angular directive scope
angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...
- [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2
Just like passing in an array to *ngFor, you can pass in any value into your structural directive s ...
- [Angular Directive] Implement Structural Directive Data Binding with Context in Angular
Just like in *ngFor, you're able to pass in data into your own structural directives. This is done b ...
- Angular项目构建指南 - 不再为angular构建而犹豫不决(转)
如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余 ...
- [Angular 2 Router] Configure Your First Angular 2 Route
Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and ...
- 【Angular专题】——(1)Angular,孤傲的变革者
目录 一. 漫谈Angular 二. 如果你还在使用Angularjs 三. 我计划这样学习Angular技术栈 一. 漫谈Angular Angular,来自Google的前端SPA框架,与Reac ...
- [Angular] Use Angular components in AngularJS applications with Angular Elements
When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...
- Angular 从入坑到挖坑 - Angular 使用入门
一.Overview angular 入坑记录的笔记第一篇,完成开发环境的搭建,以及如何通过 angular cli 来创建第一个 angular 应用.入坑一个多星期,通过学习官方文档以及手摸手的按 ...
随机推荐
- 43.c++指针类型转换
数据类型转换(static_cast) //数据类型转换 printf("%d\n", static_cast<int>(10.2)); 指针类型转换(reinterp ...
- HUSTOJ 1072 小数背包问题
HUSTOJ 1072 小数背包问题 题目描述 有一个背包,背包容量是M(0<M≤500),有N(1<N≤1000)个物品,物品可以分割成任意大小. 要求尽可能让装入背包中的物品总价值最大 ...
- jsp+tomcat+ 创建project 配置project
*如今我们已经下载到了 tomcat 7.0+ eclipse for java ee 直接解压,打开eclipse. 接下来是步骤: eclipse 打开的界面.空空如也 ! ..* 点击 file ...
- 计科1111-1114班第一次实验作业(NPC问题——回溯算法、聚类分析)
实验课安排 地点: 科技楼423 时间: 计科3-4班---15周周一上午.周二下午 计科1-2班---15周周一下午.周二晚上(晚上时间从18:30-21:10) 请各班学委在实验课前飞信通知大家 ...
- Delphi部份函数,命令,属性中文说明
Abort 函数 引起放弃的意外处理 Abs 函数 绝对值函数 AddExitProc 函数 将一过程添加到运行时库的结束过程表中 Addr 函数 返回指定对象的地址 AdjustLineBreaks ...
- textview-调节字体间距跟行距
直接进行代码验证 1)当没有添加字体间距属性的时候 <TextView android:id="@+id/text_view" android:layout_width=&q ...
- android 4.4 添加物理按键
kernel下添加 Linux-3.4/drivers/input/keyboard/Makefile linux-3.4/drivers/input/keyboard/sw-keyboard.c s ...
- JS/CSS 全屏幕导航 – 从上到下动画
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- cocos2d-x中六种持续性动作
持续性动作: (一) 位置变化动作 Move by to Jump by to (二) 属性变化动作 Scale by to Rotate by to Fade in out to Tint to b ...
- bootstrap课程5 bootstrap中的组件使用的注意事项是什么
bootstrap课程5 bootstrap中的组件使用的注意事项是什么 一.总结 一句话总结: 1.img-responsive的作用是什么(其实还是要多看手册)? 看起来像width=100%的效 ...