// #docregion import { Directive, OnInit, OnDestroy } from '@angular/core'; import { LoggerService } from './logger.service'; let nextId = 1; // #docregion spy-directive // Spy on any element to which it is applied. // Usage:
...
@Directive({selector: '[mySpy]'}) export class SpyDirective implements OnInit, OnDestroy { constructor(private logger: LoggerService) { } ngOnInit() { this.logIt(`onInit`); } ngOnDestroy() { this.logIt(`onDestroy`); } private logIt(msg: string) { this.logger.log(`Spy #${nextId++} ${msg}`); } } // #enddocregion spy-directive