[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2
A @Directive is used to add behavior to elements and components in your application. This makes @Directives ideal for behaviors such as "tracking" which don't belong in a Component, but do belong as a behavior in your application.
import {Directive, HostListener, Input} from '@angular/core';
import {TrackingService} from "../services/tracking.service";
@Directive({
selector: '[track]'
})
export class TrackDirective {
@Input() track;
constructor(private trackingService: TrackingService) { }
@HostListener('click', ['$event']) onClick(event) {
this.trackingService.tracking(
event,
this.track
)
}
}
import { Injectable } from '@angular/core';
@Injectable()
export class TrackingService {
logs = [];
constructor() { }
tracking(event, log) {
this.logs.push({
event,
log
});
console.log(this.logs)
}
}
<button [track]="'one is clicked'">One</button>
<button [track]="'two is clicked'">Two</button>
<button [track]="'three is clicked'">Three</button>
[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2的更多相关文章
- Angular之 Scope和 Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- Angular自定义指令(directive)
angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...
- angular的GitHub Repository Directive Example学习
angular的GitHub Repository Directive Example学习 <!DOCTYPE html> <html ng-app="myApp" ...
- angular自定义指令-directive
Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. v ...
- Angular 2的12个经典面试问题汇总(文末附带Angular测试)
Angular作为目前最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手,还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...
- Angular 2的12个经典面试问题汇总(文末附带Angular測试)
Angular作为眼下最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手.还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...
- -_-#【Angular】自定义指令directive
AngularJS学习笔记 <!DOCTYPE html> <html ng-app="Demo"> <head> <meta chars ...
- [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 Directive] Create a Template Storage Service in Angular 2
You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. Y ...
随机推荐
- Zabbix监控告警
一 钉钉告警 1.1.1 添加钉钉机器人 发起群聊 创建完群聊选择,机器人管理 选择你要绑定的群聊 复制下面地址留用 1.1.2 编写钉钉告警脚本 安装requests库,HTTP客户端, # yum ...
- 洛谷 P1313 计算系数
题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...
- ByteUtils
package sort.bing.com; import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import j ...
- the way to go
推荐两本书 关于go语言的 The way to go Go语言编程高清完整版电子书.pdf
- Javascript 6 种继承
1.原型链继承 // 1.原型链继承的两个问题===>在借用构造函数中可以解决下下面的两个问题//problem: 在创建子类型的实例时,不能向超类型的实例传递参数(在这里就是不能向A()里传递 ...
- Codeforces 919F. A Game With Numbers(博弈论)
Imagine that Alice is playing a card game with her friend Bob. They both have exactly 88 cards and ...
- 解决sublime text3配置Python3编译环境:运行代码时提示“NO Build System”
只需要在路径中把单杠换成双杠,重启sublime即可.
- 【2017 Multi-University Training Contest - Team 9】Numbers
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6168 [题意] 有一个长度为n的序列a1--an,根据a序列生成了一个b序列,b[i] = a[i]+a ...
- 淘宝分类导航条;纯css实现固定导航栏
效果例如以下: 页面例如以下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- elasticsearch cluster 详解
上一篇通过clusterservice对cluster做了一个简单的概述, 应该能够给大家一个初步认识.本篇将对cluster的代码组成进行详细分析,力求能够对cluster做一个更清晰的描述.clu ...