[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2

@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>
时间: 2025-01-23 03:31:07

[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2的相关文章

详说Angular之指令(directive)

前言 angular核心部分如下图几大块,最重要的莫过于指令这一部分,本文将重点讲解指令这一部分,后续笔者将通过陆续的学习来叙述其他如:factory.service等,若有叙述错误之处,欢迎各位指正以及批评.本文将通过一些实例来进行叙述. 话题 restrict以及replace 在sublimeText中安装angular插件之后,我们需要创建指令时此时将自动出现如下定义:所以我们将重点放在如下各个变量的定义. .directive('', ['', function(){ // Runs

angular的GitHub Repository Directive Example学习

angular的GitHub Repository Directive Example学习 运行下面代码 <!DOCTYPE html><html ng-app="myApp"><head>     <meta charset="utf-8" />     <title>GitHub Repository Directive Example</title>       <script sr

angular自定义指令-directive

Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. var myDirective = angular.module('directives', []); myDirective.directive('directiveName', function($inject) { return { template: '<div></div>',

angular ng build 报错 Cannot read property &#39;default&#39; of undefined

95% emitting index-html-webpack-plugin Cannot read property 'default' of undefinedTypeError: Cannot read property 'default' of undefined at compiler.hooks.emit.tapPromise (E:\projects\node_modules\@angular-devkit\build-angular\src\angular-cli-files\p

Angular学习心得之directive——require选项的细节

谈require选项之前,应该先说说controller选项,controller选项允许指令对其他指令提供一个类似接口的功能,只要别的指令(甚至是自己)有需要,就可以获取该controller,将其作为一个对象,并取得其中的所有内容.而require就是连接两个指令的锁链,它可以选择性地获取指令中已经定义好的controller,并作为link函数的第四个参数传递进去,link函数的四个参数分别为scope,element,attr和someCtrl,最后一个就是通过require获取的con

[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. You can store these TemplateRefs in a Service and then access them from any @Directive or @Component in your app. We want to create a service and a componen

[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 by declaring the variable using a let statement then passing context into the createEmbeddedView call. We know *ngFor we did like this: *ngFor="let mes

Angular自定义指令(directive)

angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content=&quo

Angular学习心得之directive——scope选项与绑定策略

开门见山地说,scope:{}使指令与外界隔离开来,使其模板(template)处于non-inheriting(无继承)的状态,当然除非你在其中使用了transclude嵌入,这点之后的笔记会再详细记录的.但是这显然不符合实际开发中的需求,因为实际上,我们经常想要我们的指令能够在特定的情况下与外界进行数据上的交互,这就需要借助绑定策略之手了. 大家知道,当scope选项写为scope:{}这种形式的时候,就已经为指令生成了隔离作用域,现在,我们来看看绑定策略的三种形式:& .= .@. 首先是