2015-11-21 14:23:40 -05:00
|
|
|
// #docregion
|
2016-06-07 19:06:25 -04:00
|
|
|
import { Directive, OnInit, OnDestroy } from '@angular/core';
|
2016-05-03 08:06:32 -04:00
|
|
|
|
2016-06-07 19:06:25 -04:00
|
|
|
import { LoggerService } from './logger.service';
|
2015-11-21 14:23:40 -05:00
|
|
|
|
|
|
|
let nextId = 1;
|
|
|
|
|
2016-01-23 13:21:09 -05:00
|
|
|
// #docregion spy-directive
|
2015-11-21 14:23:40 -05:00
|
|
|
// Spy on any element to which it is applied.
|
2016-05-01 15:27:19 -04:00
|
|
|
// Usage: <div mySpy>...</div>
|
|
|
|
@Directive({selector: '[mySpy]'})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class SpyDirective implements OnInit, OnDestroy {
|
2015-11-21 14:23:40 -05:00
|
|
|
|
2016-05-03 08:06:32 -04:00
|
|
|
constructor(private logger: LoggerService) { }
|
2015-11-21 14:23:40 -05:00
|
|
|
|
2016-05-03 08:06:32 -04:00
|
|
|
ngOnInit() { this.logIt(`onInit`); }
|
2015-11-21 14:23:40 -05:00
|
|
|
|
2016-05-03 08:06:32 -04:00
|
|
|
ngOnDestroy() { this.logIt(`onDestroy`); }
|
2015-11-21 14:23:40 -05:00
|
|
|
|
2016-05-03 08:06:32 -04:00
|
|
|
private logIt(msg: string) {
|
|
|
|
this.logger.log(`Spy #${nextId++} ${msg}`);
|
2015-11-21 14:23:40 -05:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 13:21:09 -05:00
|
|
|
// #enddocregion spy-directive
|